Python Ansible:如何在“stat”提供的字典中迭代路径?
我试图使用ansible来确定存在哪些路径集合,然后只对存在的路径进行操作(不仅仅是下面的示例中的Python Ansible:如何在“stat”提供的字典中迭代路径?,python,ansible,Python,Ansible,我试图使用ansible来确定存在哪些路径集合,然后只对存在的路径进行操作(不仅仅是下面的示例中的复制,还可以进一步编辑)。 这是我想到的——这是第二次迭代,在第二次任务中,我也无法使用和_item获得任何东西 - name: disable plasma - identify files to act on stat: path: "{{ item }}" register: plasma_conf with_items: - "/usr/share/aut
复制
,还可以进一步编辑)。
这是我想到的——这是第二次迭代,在第二次任务中,我也无法使用和_item
获得任何东西
- name: disable plasma - identify files to act on
stat:
path: "{{ item }}"
register:
plasma_conf
with_items:
- "/usr/share/autostart/plasma-desktop.desktop"
- "/usr/share/autostart/plasma-netbook.desktop"
- "/etc/xdg/autostart/plasmashell.desktop"
- name: disable plasma - copy config locally
copy:
src: "{{ item.item }}"
dest: "/home/{{ main_user }}/.config/autostart/{{ item.item | basename }}"
with_dict:
plasma_conf.results
when:
item.stat.exists == true
复制任务失败,
fatal:[localhost]=>with\u dict需要一个dict
。如何基于stat
提供的dict
结构来完成这项工作?plasma\u conf。results
是一个统计字典列表。在第二个任务中,将替换为\u dict
,将替换为\u items
。请参阅,当然还有udondan建议的调试输出。感谢您的所有评论-debug
和with_items
之前已经在多次迭代中使用过,但未能生成我想要的输出。我回到过去,使用了久经考验的“放弃一切,从头开始”的方法,并使用了以下极简主义、独立的示例:
---
- hosts:
localhost
tasks:
- name:
create testing infrastructure a)
file:
path: "/tmp/{{ item }}"
state: touch
mode: 744
with_items:
- testFileA
- testFileB
- name:
create testing infrastructure b)
file:
path: "/tmp/testDir"
state: directory
mode: 744
- name:
identify files to act on
stat:
path: "{{ item }}"
register:
files2move
with_items:
- "/tmp/testFileA"
- "/tmp/testFileB"
- "/tmp/testFileC"
- name:
copy available files
copy:
src: "{{ item.item }}"
dest: "/tmp/testDir/{{ item.item | basename }}"
mode: 640
with_items:
files2move.results
when:
item.stat.exists == true
我真的不知道现在有什么不同,但这是可行的-无论是在极简版本中,还是当移植回我的代码中时…注册的结果应该是一个dict列表,而不是dict,因此带有_项的
是正确的。项目的有什么问题?如果您还没有这样做,您可以在调试任务中输出plasma_conf
,以更好地理解您正在使用的数据结构:-debug:var=plasma_conf
,正如@udondan所说的那样-使用-debug:var=plasma_conf
是帮助您理解所需内容的最好方法。许多任务在寄存器变量中返回大量不同的信息,因此了解可用信息的唯一方法是使用调试任务将其打印出来并查看。