繁体   English   中英

Ansible 剧本:在变量中循环

[英]Ansible playbook : loop in variable

我想创建一个循环来检查文件是否存在于特定位置。

这是我的ansible剧本:

- name: Launch purge script
  hosts: roi-asb-01p
  vars_files:
    - instance_test.yml

  tasks:
    - name: Check if file test.php exists
      stat:
        path: "/home/alekso/instance_test/prod/{{ instance_all }}/test.php"
      register: result
        #  loop: "{{ instance_all }}"
    - name : DEBUG FILE EXISTS
      debug:
        msg: "The file test.php exist"
      when: result.stat.exists
      loop: "{{ instance_all }}"
    - name: DEBUG FILE NOT EXISTS
      debug:
        msg: "The file test.php doesn't exist"
      when: not result.stat.exists
      loop: "{{ instance_all }}"

问题是对于路径,它在我的 instance_test.yml 文件中包含我的“instance_all”变量的每个实例,因此它检测到不存在文件,因为路径不好

路径突然看起来像这样: /home/test/instance_test/prod/[one, two, tree]/test.php而不是/home/test/instance_test/prod/one/test.php等...

这是我的不同变量的文件:

instance_all:
  - one
  - two
  - tree

instance_1:
  - test1

instance_2:
  - test2

这是命令输出:

test@srv-test-1:~/ansible/test$ ansible-playbook -i inventaire.ini launch-purge-script-test.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: An error occurred while calling ansible.utils.display.initialize_locale (unsupported locale setting). This may result in incorrectly calculated text widths
that can cause Display to print incorrect line lengths

PLAY [Launch purge script] ***********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [roi-asb-01p]

TASK [Check if file test.php exists] *************************************************************************************************************************************
ok: [roi-asb-01p]

TASK [debug] *************************************************************************************************************************************************************
skipping: [roi-asb-01p] => (item=one) 
skipping: [roi-asb-01p] => (item=two) 
skipping: [roi-asb-01p] => (item=tree) 
skipping: [roi-asb-01p]

TASK [debug] *************************************************************************************************************************************************************
ok: [roi-asb-01p] => (item=one) => {
    "msg": "The file test.php doesn't exist"
}
ok: [roi-asb-01p] => (item=two) => {
    "msg": "The file test.php doesn't exist"
}
ok: [roi-asb-01p] => (item=tree) => {
    "msg": "The file test.php doesn't exist"
}

PLAY RECAP ***************************************************************************************************************************************************************
roi-asb-01p                : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  

感谢帮助

变量instance_all包含您要循环的所有元素。 但是每次迭代都需要一个元素。 这包含在 loop_var 中(默认情况下它的名称是item )。 这将在你的情况下做到这一点:

path: /home/test/instance_test/prod/{{ item }}/test.php

当然,您还应该从带有# loop:的行中删除注释符号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM