Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Ansible字典/哈希键作为特殊变量_Python_Python 3.x_Dictionary_Ansible_Ansible Facts - Fatal编程技术网

Python Ansible字典/哈希键作为特殊变量

Python Ansible字典/哈希键作为特殊变量,python,python-3.x,dictionary,ansible,ansible-facts,Python,Python 3.x,Dictionary,Ansible,Ansible Facts,我试图将Ansible事实设置为dict/hash,但希望使用一个特殊变量作为键。在我的例子中,我想使用特殊变量inventory\u hostname。但是,当我尝试此操作时,该值将作为字符串返回,而不是主机名计算机的实际名称。如何设置特殊变量inventory\u hostame的键 我试过下面的方法,但没有用 设定事实: set_fact: result_dict: "{{inventory_hostname}}": 'Linux' set_fact:

我试图将Ansible事实设置为dict/hash,但希望使用一个特殊变量作为键。在我的例子中,我想使用特殊变量
inventory\u hostname
。但是,当我尝试此操作时,该值将作为字符串返回,而不是主机名计算机的实际名称。如何设置特殊变量
inventory\u hostame
的键

我试过下面的方法,但没有用

设定事实:

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

  set_fact:
    result_dict:
      inventory_hostname: 'Linux'
返回的不是实际的清单主机名,而是字符串值

电流输出:

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

您可以这样编写任务:

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

    - debug:
        var: result_dict
这似乎对我有用