如何在循环中存储变量 Ansible 的值?

如何在循环中存储变量 Ansible 的值?

我想将的值存储{{ item }}loop一个变量中。

这是我的任务:

include_role:
    name: role1
vars:
    template_path: "inventories/{{ ENV }}/group_vars/all"
    template_name: "wlc_wlb"
loop: "{{ range(1, ihs_number+2) | list }}"

答案1

我想将的值存储{{ item }}loop一个变量中。

最小示例剧本

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - debug:
      msg: "{{ index }}"
    vars:
      index: "{{ item }}"
    loop: "{{ range(0, 10, 1) }}"

将导致输出

TASK [debug] *****************
ok: [localhost] => (item=0) =>
  msg: '0'
ok: [localhost] => (item=1) =>
  msg: '1'
ok: [localhost] => (item=2) =>
  msg: '2'
ok: [localhost] => (item=3) =>
  msg: '3'
ok: [localhost] => (item=4) =>
  msg: '4'
ok: [localhost] => (item=5) =>
  msg: '5'
ok: [localhost] => (item=6) =>
  msg: '6'
ok: [localhost] => (item=7) =>
  msg: '7'
ok: [localhost] => (item=8) =>
  msg: '8'
ok: [localhost] => (item=9) =>
  msg: '9'

文档

相关内容