Ansible:- loop_var 不工作,值未定义

Ansible:- loop_var not working, value undefined

我正在尝试使用循环访问变量,但它对我不起作用。

剧本

---
- name: test looping
  hosts: localhost
  gather_facts: False
  vars:
    repos:
      - name: repo1
        os_list:
          - centos
          - rhel
        major_distribution_list:
          - 6
          - 7
          - 8
        archs:
          - noarch
          - x86_64
  tasks:

    - include_tasks: repo-paths.yml
      with_items: "{{ repos }}"
      loop_control:
        loop_var: repo

    - debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}/{{ item.3 }}"
      with_nested:
        - "{{ repo.os_list }}"
        - "{{ repo.major_distribution_list }}"
        - "{{ repo.name }}"
        - "{{ repo.archs }}"

但我收到错误消息,repo 变量未定义。 原始输出:-

PLAY [test looping] ***********************************************************************************************************************************************************

TASK [include_tasks] **********************************************************************************************************************************************************

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

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

预期输出:-

"msg": "./centos/6/repo1/noarch"
"msg": "./centos/6/repo1/x86_64"
"msg": "./centos/7/repo1/noarch"
"msg": "./centos/7/repo1/x86_64"
"msg": "./centos/8/repo1/noarch"
"msg": "./centos/8/repo1/x86_64"
"msg": "./rhel/6/repo1/noarch"
"msg": "./rhel/6/repo1/x86_64"
"msg": "./rhel/7/repo1/noarch"
"msg": "./rhel/7/repo1/x86_64"
"msg": "./rhel/8/repo1/noarch"
"msg": "./rhel/8/repo1/x86_64"

请帮助我,也请告诉我是否有其他方法可以做到这一点。

您需要输入:

    - debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}/{{ item.3 }}"
      with_nested:
        - "{{ repo.os_list }}"
        - "{{ repo.major_distribution_list }}"
        - "{{ repo.name }}"
        - "{{ repo.archs }}"

到名为 repo-paths.yml 的文件中。这是因为 with_items: "{{ repos }}" 在单个任务 include_tasks: repo-paths.yml 上运行循环并将 repo 变量传递给导入文件中的任务。循环不会影响同一文件中的后续任务。

loop_control 语句是为第一个任务定义的,并且只与它相关。它没有贯穿剧本的其余部分。如果要在with_nested任务中使用,需要再次声明。

你遇到的第二个问题是with_nested。您的 repos 变量是一个包含一个元素的列表。为了支持遍历列表,您应该在它自己的文件中定义 debug 任务,并在列表中使用 include_tasksloop。像这样

---
- name: test looping
  hosts: localhost
  gather_facts: False
  vars:
    repos:
      - name: repo1
        os_list:
          - centos
          - rhel
        major_distribution_list:
          - 6
          - 7
          - 8
        archs:
          - noarch
          - x86_64
  tasks:
  - include_tasks: my_tasks.yml
    loop: "{{ repos }}"
    loop_control:
      loop_var: repo

--- my_tasks.yml

- debug: msg="./{{ item.0 }}/{{ item.1 }}/{{ item.2 }}"
  with_nested:
    - "{{ repo.os_list }}"
    - "{{ repo.major_distribution_list }}"
    - "{{ repo.archs }}"

--- output 

ok: [localhost] => (item=[u'centos', 6, u'noarch']) => {
    "msg": "./centos/6/noarch"
}
ok: [localhost] => (item=[u'centos', 6, u'x86_64']) => {
    "msg": "./centos/6/x86_64"
}
ok: [localhost] => (item=[u'centos', 7, u'noarch']) => {
    "msg": "./centos/7/noarch"
}
..
..