ansible - 需要在 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
解决方案
你的剧本有点错误。您正在尝试使用 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
推荐阅读
- python - 跟随我的鼠标但停留在圆圈上的弧线
- wordpress - 如何在wordpress中更改邮件到电子邮件
- python - 如何构建一个聊天机器人来回复相同的常见问题解答问题,该问题对不同公司的常见问题解答数据在 Rasa,Python 中具有不同的答案
- python - Crontab 启动 Python 脚本,但 SystemD 没有?
- azure - Azure AppInsights 中的 POST UpdateStuff [版本] 和 POST /api/v1/UpdateStuff 有什么区别
- reactjs - 无法在渲染中的方法内显示承诺数据
- javascript - 返回函数并作为表达式分配给变量的函数调用问题
- sql-server - 授予 SQL Sever 访问权限以连接到网络共享 - 无域
- android - 如何从 strings.xml 获取 DB sqlite 数据,所以当我更改语言环境时,所有数据都可以翻译
- java - 如何在没有鼠标/拖动/等的情况下切换到新场景。事件?