简体   繁体   中英

Ansible playbook : loop in variable

I want to create a loop that checks if a file exists at a specific location.

Here is my ansible playbook:

- 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 }}"

the problem is that for the path, it includes each instance of my "instance_all" variable in my instance_test.yml file so it detects that no file exists because the path is not good

The path suddenly looks like this: /home/test/instance_test/prod/[one, two, tree]/test.php instead of /home/test/instance_test/prod/one/test.php etc...

And here is my file with my different variables :

instance_all:
  - one
  - two
  - tree

instance_1:
  - test1

instance_2:
  - test2

Here is the command output :

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  

Thanks for help

The variable instance_all contains all elements that you want to loop over. But you need one single elemet in each iteration. This is contained in the loop_var (by default its name is item ). This would do it in your case:

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

Of course, you should also remove the comment symbol from the line with # loop: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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