Ansible

如何在 Ansible 中循環數組變數

  • January 18, 2022

我有以下任務,我無法為數組變數添加循環。 ShopID 被定義為 group_vars 內的一個數組。我的目標是一個一個地呼叫每個變數。

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

上述任務的輸出是這樣的 -

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}

將任務放入文件中,例如

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

並將其包含在循環中

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

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

引用自:https://serverfault.com/questions/1090388