我正在尝试利用位于 3 个数据库主机中的 1 个上的本地 Ansible 事实来设置全局事实,以便其他节点可以利用该 IP。
3 个节点,3 组本地事实,将 ansible_local.edb.type 设置为:
- 主人
- 证人
- 待机
Node2 有 ansible_local.edb.type == 备用,我正在尝试全局set_fact IP 地址。我的问题是,要有条件地设定一个事实,那么什么时候必须是:
什么时候:ansible_local.edb.type == "待机">
但这只是在特定节点上设置事实,显然是由于条件
我尝试阻止块内的任务以获取管道输出,但似乎破坏了语法 - 如果可能的话,这将是 Ansible 的一个非常酷的补充。
- debug:
msg: "{{ ansible_local.edb.type }}"
- name: Set DB standby IP address fact
set_fact:
db_standby_node_ip: "{{ hostvars[inventory_hostname][prod_nic]['ipv4']['address'] }}"
when: ansible_local.edb.type == "standby"
- debug:
msg: "{{ db_standby_node_ip }}"
TASK [debug]
*******************************************************************
task path: /path/to/playbook.yml
ok: [Node1] => {
"msg": "master"
}
ok: [Node2] => {
"msg": "standby"
}
ok: [Node3] => {
"msg": "witness"
}
TASK [Set DB standby IP address fact]
******************************************
task path: /path/to/playbook.yml
ok: [Node2] => {
"ansible_facts": {
"db_standby_node_ip": "x.x.x.y"
},
"changed": false,
"invocation": {
"module_args": {
"db_standby_node_ip": "x.x.x.y"
},
"module_name": "set_fact"
}
}
TASK [debug]
*******************************************************************
task path: /path/to/playbook.yml
fatal: [Node1]: FAILED! => {
"failed": true,
"msg": "the field 'args' has an invalid value,..
}
fatal: [Node3]: FAILED! => {
"failed": true,
"msg": "the field 'args' has an invalid value,...
}
ok: [Node2] => {
"msg": "x.x.x.y"
}
我希望有人可能对如何在全球范围内设定事实有不同的看法,但基于本地事实?
json_query
获取以 standby == "true"
作为过滤器的主机,无需使用 inventory_hostname
。
- name: Set DB standby IP address fact
set_fact:
db_standby_node_ip: {{ hostvars | json_query('* | [?ansible_local.edb.type==`standby`] | [0].'+prod_nic+'.ipv4.address') }}"