Ansible - 如何循环注册
问题描述:
我正在使用模块stat来检查某些文件是否存在于远程服务器中,并将其结果注册到变量中。即我正在查找的配置文件:conf_dev.conf,conf_pred.conf,conf_pro.conf。Ansible - 如何循环注册
稍后,我使用复制模块仅使用基于存储在 variable.results {{(ITEM)index}}。stat.exists中的属性使用条件来丢失文件。 如果文件存在,则返回TRUE,否则返回FALSE。
对于正常运行由于寄存器变量是一个数组有一个索引存储每个文件的结果我问,所以我不知道如何转换索引中的项目(0,1,2,3 )
有谁知道如何获得项目的索引?我已经试过这(看最后一行):
- name: Checking if common configuration files exists
stat:
path: "{{HOME_COMUN_CONFIG}}/{{item}}"
with_items: "{{LIST_COMMON_CONFIGURATION_ARTIFACTS}}"
register: store_results
- name: debug existe_app_comun
debug:
var: store_results
- name: Deploying missing files
copy:
src: "{{DIRTEMP_COMUN_CONFIG}}/{{item}}"
dest: "{{HOME_COMUN_CONFIG}}/{{item}}"
with_items: "{{LIST_COMMON_CONFIGURATION_ARTIFACTS}}"
when: existe_app_comun.results.{{index(item)}}.stat.exists is defined
答
为什么你遍历LIST_COMMON_CONFIGURATION_ARTIFACTS
如果你要循环注册变量?
- name: Deploying missing files
copy:
src: "{{DIRTEMP_COMUN_CONFIG}}/{{ item.item }}"
dest: "{{HOME_COMUN_CONFIG}}/{{ item.item }}"
with_items: "{{ existe_app_comun.results }}"
when: item.stat.exists
这里item
是结果的元件和item.item
是原本的环的元素。
P.S.如果目标文件夹中的文件没有被修改,并且应该与DIRTEMP_COMUN_CONFIG
中的文件相同,那么您不能使用stat
+ copy
,而只是使用copy
- 因为它是幂等的,不会复制同一个文件两次。