在 Ansible 中如何在遍历剧本中的数组时执行角色

In Ansible how to execute a role while looping over an array in the playbook

我想遍历数组并将每个数组元素值传递给 playbook 中的角色,但它在 ansible 中不起作用,有人能帮忙吗


---
#play book
- name: create config for instance
  hosts: all
  vars:
    LIST: [Asia, Americas, Artic, Antartic ,Oceania,Europe,Africa]
  connection: local
  roles:
    - role: create_config
      debug:
        msg : "{{ item }}"
      vars:
        VENUE: "{{ item }}"

      with_items:
        - "{{ LIST }}"


## Role
- name: create directory structure
  file:
    path: "{{item}}"
    state: directory
    mode: 0755
  with_items:
    - "{{dest_folder}}/{{instance_name}}/{{VENUE}}"




我低于错误

ansible-playbook  -i inventory/AlgoTest_SP  create_pkg_1.yml 

PLAY [create config for instance] **************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [localhost]

TASK [create_config : create directory structure] *******************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "{{ item }}: 'item' is undefined"}

PLAY RECAP ****************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

为了能够循环角色,您需要 include_role 任务,如:

- name: create config for instance
  hosts: localhost
  vars:
    LIST: [Asia, Americas, Artic, Antartic ,Oceania,Europe,Africa]
  tasks:
    - include_role: 
        name: create_config
      vars:
        VENUE: "{{ item }}"
      with_items:
        - "{{LIST}}"

cat roles/create_config/tasks/main.yml 
- debug:
    msg: "{{VENUE}}"

- name: create directory structure
  file:
    path: "{{item}}"
    state: directory
    mode: 0755
  with_items:
    - "{{inventory_hostname}}/{{VENUE}}"

导致:

$ ansible-playbook  69045121.yml 
PLAY [create config for instance] ********************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [include_role : create_config] ******************************************************************************************************************************************************************************************************************************************************************************************

TASK [create_config : debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Asia"
}

TASK [create_config : create directory structure] ****************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=localhost/Asia)

TASK [create_config : debug] *************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Americas"
}

[...] and so on

最后一点,您可以而且应该以不同的方式处理这个问题。此外,您在外部(剧本)和内部(角色)with_items 的 item 变量名中存在冲突 with_items,您可以使用 loop_var 设置不同的循环变量名。