【问题标题】:Ansible Dictionary/Hash Key as Special VariableAnsible 字典/哈希键作为特殊变量
【发布时间】:2019-12-13 21:00:35
【问题描述】:

我正在尝试将 Ansible 事实设置为 dict/hash,但希望使用特殊变量作为键。就我而言,我想使用特殊变量inventory_hostname。但是,当我尝试此操作时,该值将作为字符串返回,而不是主机名机器的实际名称。如何改为将键设置为特殊变量inventory_hosthame

我尝试了以下方法,但无济于事。

设置事实:

  set_fact:
    result_dict:
      "{{inventory_hostname}}": 'Linux'

  set_fact:
    result_dict:
      inventory_hostname: 'Linux'

返回的不是实际的inventory_hostname,而是字符串值。

电流输出:

    ok: [host-a] => {
    "result_dict": {
        "{{inventory_hostname}}": "Linux"
    }
}

【问题讨论】:

    标签: python python-3.x dictionary ansible ansible-facts


    【解决方案1】:

    你可以这样写你的任务:

    - hosts: localhost
      tasks:
        - set_fact:
            result_dict: "{{ {inventory_hostname: 'Linux'} }}"
    
        - debug:
            var: result_dict
    

    这似乎对我有用。

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2016-05-23
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2013-01-16
      • 1970-01-01
      相关资源
      最近更新 更多