【问题标题】:Trying to set global Ansible fact based on ansible_local fact尝试根据 ansible_local 事实设置全局 Ansible 事实
【发布时间】:2019-02-13 13:42:59
【问题描述】:

我正在尝试利用位于 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"
}

我希望有人可能对如何在全球范围内设定事实有不同的看法,但要基于本地事实?

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    使用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') }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多