Score:1

How to loop an array variable in Ansible

eg flag

I have below task and I am failing to add the loop for the array variable. ShopID is defined as an array inside group_vars. My aim is to call each variable one by one.

- name: Create directories with required permission
  file:
    path: "{{ item.dest }}"
    mode: "{{item.mode}}"
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
    recurse: yes
    state: directory
  loop:
  - { dest: '/home/{{ ShopID }}', mode: '0755', owner: 'root', group: 'root' }
  - { dest: '/home/{{ ShopID }}/.ssh', mode: '0700', owner: "{{ ShopID }}", group: "{{ ShopID }}" }
  - { dest: '/home/{{ ShopID }}/to_xxx', mode: '0777', owner: "{{ ShopID }}", group: "{{ ShopID }}" }
  - { dest: '/home/{{ ShopID }}/to_xxx/known_customers', mode: '0777', owner: "{{ ShopID }}", group: "{{ ShopID }}" }

Output for the above task is something like this-

failed: [file.xyz.com] (item={'dest': '/home/[77712351, 77712352]/.ssh', 'mode': '0700', 'owner': [77712351, 77712352], 'group': [77712351, 77712352]}) => {"ansible_loop_var": "item", "changed": false, "gid": 0, "group": "root", "item": {"dest": "/home/[77712351, 77712352]/.ssh", "group": [77712351, 77712352], "mode": "0700", "owner": [77712351, 77712352]}, "mode": "0755", "msg": "chown failed: failed to look up user [77712351, 77712352]", "owner": "root", "path": "/home/[77712351, 77712352]/.ssh", "size": 4096, "state": "directory", "uid": 0}
Score:1
br flag

Put the task into a file, e.g.

shell> cat create_dir.yml
- name: Create directories with required permission
  debug:
    msg: |
      path: "{{ item.dest }}"
      mode: "{{item.mode}}"
      owner: "{{ item.owner }}"
      group: "{{ item.group }}"
      recurse: yes
      state: directory
  loop:
    - {dest: '/home/{{ ShopID }}', mode: '0755', owner: 'root', group: 'root'}
    - {dest: '/home/{{ ShopID }}/.ssh', mode: '0700', owner: "{{ ShopID }}", group: "{{ ShopID }}"}
    - {dest: '/home/{{ ShopID }}/to_xxx', mode: '0777', owner: "{{ ShopID }}", group: "{{ ShopID }}"}
    - {dest: '/home/{{ ShopID }}/to_xxx/known_customers', mode: '0777', owner: "{{ ShopID }}", group: "{{ ShopID }}"}

and include it in the loop

shell> cat test-496.yml
- hosts: localhost
  vars:
    ShopID_list: [77712351, 77712352]
  tasks:
    - include_tasks: create_dir.yml
      loop: "{{ ShopID_list }}"
      loop_control:
        loop_var: ShopID

gives

TASK [Create directories with required permission] *************************

  msg: |-
    path: "/home/77712351"
    mode: "0755"
    owner: "root"
    group: "root"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712351/.ssh"
    mode: "0700"
    owner: "77712351"
    group: "77712351"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712351/to_xxx"
    mode: "0777"
    owner: "77712351"
    group: "77712351"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712351/to_xxx/known_customers"
    mode: "0777"
    owner: "77712351"
    group: "77712351"
    recurse: yes
    state: directory

TASK [Create directories with required permission] *************************

  msg: |-
    path: "/home/77712352"
    mode: "0755"
    owner: "root"
    group: "root"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712352/.ssh"
    mode: "0700"
    owner: "77712352"
    group: "77712352"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712352/to_xxx"
    mode: "0777"
    owner: "77712352"
    group: "77712352"
    recurse: yes
    state: directory

  msg: |-
    path: "/home/77712352/to_xxx/known_customers"
    mode: "0777"
    owner: "77712352"
    group: "77712352"
    recurse: yes
    state: directory
Score:0
nf flag

Not directly answers your question, but can give a hint for those who want to pass multiple variables via one item. Text in double curly brackets is a python script, so you can use following approach:

task_name: arg1={{ item.split(',')[0] }} arg2={{ item.split(',')[1] }}
with_items:
- "a,b"
- "c,d"

Of course it is a hack, but it's easy to implement if you want something quick.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.