Ansible 节点列表中的项取决于主机组中主机的当前索引
我有下面的设置Ansible 节点列表中的项取决于主机组中主机的当前索引,ansible,Ansible,我有下面的设置 groups_var/all.yml --- cassandra_restore: nodes: - 192.168.0.1 - 192.168.0.2 - 192.168.0.3 inventory contains, [just_created] 192.168.0.4 192.168.0.5 192.168.0.6 main.yml --- # playbook - name: setup hosts: just_created
groups_var/all.yml
---
cassandra_restore:
nodes:
- 192.168.0.1
- 192.168.0.2
- 192.168.0.3
inventory contains,
[just_created]
192.168.0.4
192.168.0.5
192.168.0.6
main.yml
---
# playbook
- name: setup
hosts: just_created
remote_user: ubuntu
become: true
become_method: sudo
gather_facts: yes
vars:
current_index: "{{ ansible_play_batch.index(inventory_hostname) }}"
tasks:
- debug:
msg: "current host index: {{ ansible_play_batch.index(inventory_hostname) }} : {{ current_index }}"
- debug:
msg: "first target host: {{ cassandra_restore.nodes.0 }}"
- name: get mapped value
debug:
msg: "current target host: {{ cassandra_restore.nodes.current_index }} "
我想根据刚刚创建的主机组中主机的当前索引访问节点列表中的项。
因此,当主机为192.168.0.4时,应打印192.168.0.1;当主机为192.168.0.5时,应打印192.168.0.2,依此类推
如何实现这一点?您必须使用地图表单以动态索引或键进行访问:
- name: get mapped value
debug:
msg: "current target host: {{ cassandra_restore.nodes[current_index] }} "
但我不确定主机的顺序是确定的
---
# playbook for creating cassandra setup
- name: setup cassnadra
hosts: just_created
remote_user: ubuntu
become: true
become_method: sudo
gather_facts: yes
vars:
current_index: "{{ ansible_play_batch.index(inventory_hostname) }}"
tasks:
- name: get mapped value
debug:
msg: "current_index current target host: {{ item.index }} : {{ item.host }} : {{ current_index }} "
when: current_index == item.index
with_items:
- "{{ cassandra_restore }}"
我用以下变量得到了这个结果
cassandra_restore:
- { index: "0", host: 192.168.0.1 }
- { index: "1", host: 192.168.0.2 }
- { index: "2", host: 192.168.0.3 }
Thx的回复,但这给了我以下错误,“字段'args'有一个无效值,它似乎包括一个未定义的变量。”