需要在 Ansible 中使用宿主变量(ansible engine 2.8 & ansible tower 3.5)

Need to use host variables in Ansible (ansible engine 2.8 & ansible tower 3.5)

需要您的帮助才能实现以下目标: - 在 Ansible 清单中使用与 hostIP 内联提供的变量(即主机变量)

我的库存:

[ora_patch]
10.24.29.14 SID=orcl,orcl2

我的剧本:

---
- hosts: [ora_patch]
  tasks:
  - debug:
     var: "{{ hostvars[ansible_host]['SID'] }}"

输出**我得到**:

PLAY [ora_patch] ************************************************************

TASK [patch_ora_si_122 : debug] *****************************************
ok: [10.24.29.14] => {
    "orcl,orcl2": "(Undefined, Undefined)"
}

PLAY RECAP ******************************************************************
10.24.29.14               : ok=1    changed=0    unreachable=0    failed=0

输出**我要**:

PLAY [ora_patch] ***********************************************************

TASK [patch_ora_si_122 : debug] ****************************************
ok: [10.24.29.14] => {
    "SID": "orcl,orcl2"
}

PLAY RECAP *****************************************************************
10.24.29.14               : ok=1    changed=0    unreachable=0    failed=0

我执行的命令:

ansible-playbook -i inventory patch_ora_si_122.yml

Ansible inventory.ini 遵循 beloe 格式。

[hosts]

[hosts:vars]

下面应该有效:

[ora_patch]
10.24.29.14 SID=orcl,orcl2

[ora_patch:vars]
SID=orcl,orcl2

你的剧本有点错误。您正在尝试使用 hostvar "{{ hostvars[ansible_host]['SID'] }}" 的内容作为变量名来显示 debug: var=....

只需将您的剧本更改为

---
- hosts: ora_patch
  tasks:
  - debug:
     msg: "SID: {{ hostvars[ansible_host]['SID'] }}"

或者你也可以直接使用变量名:

---
- hosts: ora_patch
  tasks:
  - debug:
     var: SID