我想在清单查询中使用清单主机名来配置使用相同模板但变量不同的服务器
/ etc / ansible / hosts
[nodes]
node1 ansible_host=192.168.0.2
角色/角色/默认值/main.yml
node1:
if1: eth1
ip1: 1.1.1.1/11
node2:
if1: eth4
ip1: 4.4.4.4/44
roles / role / tasks / main.yml
- name: test
debug:
msg: "{{ [inventory_hostname].[if1] }} end test "
期望它在控制台上显示eth1
,但显示错误
fatal: [node1]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'if1' is undefined\n\nThe error appears to have been in '/root/ansible/roles/role/tasks/main.yml': line 6, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: test\n ^ here\n"
}
答案 0 :(得分:0)
您要寻找的是vars
:
- name: test
debug:
msg: "{{ vars[ansible_hostname]['if1'] }} end test"