如何在 Ansible 中循环数组变量

如何在 Ansible 中循环数组变量

我有以下任务,但无法为数组变量添加循环。 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}

答案1

将任务放入文件中,例如

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

答案2

虽然不能直接回答您的问题,但可以为那些想要通过一个项目传递多个变量的人提供提示。双花括号中的文本是 python 脚本,因此您可以使用以下方法:

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

当然,这是一种黑客手段,但如果你想要快速得到某些东西,它很容易实现。

相关内容