【问题标题】:In Ansible how to execute a role while looping over an array in the playbook在 Ansible 中,如何在遍历剧本中的数组时执行角色
【发布时间】:2021-11-01 19:22:05
【问题描述】:

我想遍历数组并将每个数组元素值传递给剧本中的角色,但它不能在 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   

【问题讨论】:

  • 我不是 ansible 专家,但您正在尝试使用角色调用任务并使用模块调试。

标签: arrays loops ansible roles playback


【解决方案1】:

为了能够循环角色,您需要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 变量名发生冲突,您可以使用 loop_var 设置不同的循环变量名。

【讨论】:

  • 谢谢它对我有用,虽然 TASK [creat_config : create directory structure] ********************** ****************************************************** ******************************************* [警告]:循环变量“项目”已被使用。您应该将任务的loop_control 选项中的loop_var 值设置为其他值,以避免变量冲突和意外行为。
  • 感谢 Guido,我现在通过添加 loop_var stackoverflow.com/questions/62272678/… 解决了这个警告
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多