【问题标题】:using inventory_hostname as part of a dictionary lookup使用inventory_hostname 作为字典查找的一部分
【发布时间】:2019-05-15 17:38:49
【问题描述】:

我想使用inventory_hostname 作为字典查找的一部分来配置使用相同模板但具有不同变量的服务器

/etc/ansible/hosts

[nodes]
node1 ansible_host=192.168.0.2

roles/role/defaults/main.yml

node1:
  if1: eth1
  ip1: 1.1.1.1/11

node2:
  if1: eth4
  ip1: 4.4.4.4/44

角色/角色/任务/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"
}

【问题讨论】:

    标签: ansible


    【解决方案1】:

    你要找的是vars:

    - name: test
      debug:
        msg: "{{ vars[ansible_hostname]['if1'] }} end test"
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2021-05-22
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多