Ansible,with_nested,如何在循环中分配动态变量

Ansible,with_nested,如何在循环中分配动态变量

我正在尝试动态地迭代数组中的条目数并使用输出作为索引。

很确定我做错了什么。

如何为序列末尾分配一个表示当前数组计数的变量?

ansible-playbook 2.9.6

运行.yml:

---
- hosts: localhost

  tasks:
  - name: Import config
    include_vars: 
      file: ./config.yml

  - name: DEBUG
    debug: msg="{{ item[0].team_name }}: {{ item[0].applications.name }}: index: {{ item[1] }}"
    with_nested:
      - "{{ teams }}"
      - "{{ lookup('sequence', 'start=1 end='+(item[0].applications|length))|string }}"

配置.yml:

teams:
  - team_name: Name-of-Team_A
    applications:
      - name: app_name_a
      - name: app_name_b
  - team_name: Name-of-Team_B
    applications:
      - name: app_name_c
      - name: app_name_d

执行:

ansible-playbook run.yml

PLAY [localhost] ********************************************************************************************************************************************************************

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

TASK [Import config] ****************************************************************************************************************************************************************
ok: [localhost]

TASK [DEBUG] ************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "'item' is undefined"}

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

期望的结果:

msg: 'Name-of-Team_A: app_name_a: index: 1' 
msg: 'Name-of-Team_A: app_name_b: index: 2' 
msg: 'Name-of-Team_B: app_name_c: index: 1' 
msg: 'Name-of-Team_B: app_name_d: index: 2'

答案1

A:例如下面的任务

- debug:
    msg: "{{ item.0.team_name }}: {{ item.1.name }}: {{ index1|int + 1 }}"
  with_subelements:
    - "{{ teams }}"
    - applications
  vars:
    team_names: "{{ teams|
                    map(attribute='team_name')|
                    list }}"
    index0: "{{ team_names.index(item.0.team_name) }}"
    applications: "{{ teams|
                      map(attribute='applications')|
                      list }}"
    application_names: "{{ applications[index0|int]|
                           map(attribute='name')|
                           list }}"
    index1: "{{ application_names.index(item.1.name) }}"

给出

  msg: 'Name-of-Team_A: app_name_a: 1'
  msg: 'Name-of-Team_A: app_name_b: 2'
  msg: 'Name-of-Team_B: app_name_c: 1'
  msg: 'Name-of-Team_B: app_name_d: 2'

此解决方案仅限于唯一列表。


对于此用例来说,更好的结构是字典。例如,下面的任务给出了相同的结果

- debug:
    msg: "{{ item.0.key }}: {{ item.1 }}: {{ teams[item.0.key].index(item.1) + 1 }}"
  with_subelements:
    - "{{ teams|dict2items }}"
    - value
  vars:
    teams:
      Name-of-Team_A:
        - app_name_a
        - app_name_b
      Name-of-Team_B:
        - app_name_c
        - app_name_d

列表必须是唯一的。方法指数将找到第一个匹配项。

相关内容