使用 ansible_hostnames 的 Ansible 循环
Ansible loop using ansible_hostnames
我正在尝试更新 NiFi 部署的配置文件,初始部署配置需要包含节点以允许在它们之间建立 HTTPS 连接。
我有一个 ansible 任务可以对配置文件进行必要的结构更改,但我似乎无法插入正确的详细信息。
- name: Add each host to the authorizers.xml
lineinfile:
path: /opt/nifi/conf/authorizers.xml
line: "<property name=\"Node Identity {{ item }}\">CN={{ item }}, OU=NiFi</property>"
insertafter: <!--accessPolicyProvider Node Identities-->
loop: "{{ query('inventory_hostnames', 'nifi') }}"
这是主机的 IP 地址,我需要为每个节点获取 ansible_hostname。
我玩过 ansible_play_batch 和循环:“{{ groups['nifi'] }}” 但我得到了结果,每次都输出 ip 地址而不是短主机名。
短主机名没有存储在我的 ansible 配置中的任何地方,它们是(如果我理解正确的话)在 运行 时间通过收集事实过程确定的。我真的不想将节点名称放入列表变量中。
Q: "Get the ansible_hostname for each node"
答:给定库存
shell> cat hosts
[nifi]
10.1.0.51
10.1.0.52
下面的剧本
- hosts: nifi
tasks:
- debug:
var: ansible_hostname
给出(删节)
ok: [10.1.0.51] =>
ansible_hostname: test_01
ok: [10.1.0.52] =>
ansible_hostname: test_02
可以迭代组中的主机并从主机变量中获取 ansible_hostname。例如,delegate_to localhost 和 run_once
- debug:
msg: "{{ hostvars[item].ansible_hostname }}"
loop: "{{ groups.nifi }}"
delegate_to: localhost
run_once: true
给予
ok: [10.1.0.51 -> localhost] => (item=10.1.0.51) =>
msg: test_01
ok: [10.1.0.51 -> localhost] => (item=10.1.0.52) =>
msg: test_02
我正在尝试更新 NiFi 部署的配置文件,初始部署配置需要包含节点以允许在它们之间建立 HTTPS 连接。
我有一个 ansible 任务可以对配置文件进行必要的结构更改,但我似乎无法插入正确的详细信息。
- name: Add each host to the authorizers.xml
lineinfile:
path: /opt/nifi/conf/authorizers.xml
line: "<property name=\"Node Identity {{ item }}\">CN={{ item }}, OU=NiFi</property>"
insertafter: <!--accessPolicyProvider Node Identities-->
loop: "{{ query('inventory_hostnames', 'nifi') }}"
这是主机的 IP 地址,我需要为每个节点获取 ansible_hostname。 我玩过 ansible_play_batch 和循环:“{{ groups['nifi'] }}” 但我得到了结果,每次都输出 ip 地址而不是短主机名。
短主机名没有存储在我的 ansible 配置中的任何地方,它们是(如果我理解正确的话)在 运行 时间通过收集事实过程确定的。我真的不想将节点名称放入列表变量中。
Q: "Get the ansible_hostname for each node"
答:给定库存
shell> cat hosts
[nifi]
10.1.0.51
10.1.0.52
下面的剧本
- hosts: nifi
tasks:
- debug:
var: ansible_hostname
给出(删节)
ok: [10.1.0.51] =>
ansible_hostname: test_01
ok: [10.1.0.52] =>
ansible_hostname: test_02
可以迭代组中的主机并从主机变量中获取 ansible_hostname。例如,delegate_to localhost 和 run_once
- debug:
msg: "{{ hostvars[item].ansible_hostname }}"
loop: "{{ groups.nifi }}"
delegate_to: localhost
run_once: true
给予
ok: [10.1.0.51 -> localhost] => (item=10.1.0.51) =>
msg: test_01
ok: [10.1.0.51 -> localhost] => (item=10.1.0.52) =>
msg: test_02