Python Ansible字典/哈希键作为特殊变量
我试图将Ansible事实设置为dict/hash,但希望使用一个特殊变量作为键。在我的例子中,我想使用特殊变量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:
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
这似乎对我有用