Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
如何访问从cli传递的所有ansible额外变量的列表?(变量中的变量)_Ansible_Ansible Playbook - Fatal编程技术网

如何访问从cli传递的所有ansible额外变量的列表?(变量中的变量)

如何访问从cli传递的所有ansible额外变量的列表?(变量中的变量),ansible,ansible-playbook,Ansible,Ansible Playbook,我在使用variable-of-variable-way从playbook访问额外变量时遇到问题。 例如,我创建了一个包含以下内容的group_vars/mygroup.yml文件: MYVAR=AAAA 然后我调用命令并传递额外的变量: ansible playbook-我测试playbook.yml--额外变量“MYVAR=BBB” 我需要从现有变量列表中获取MYVAR的实际值。我试着这样做: - debug: var=hostvars[inventory_hostname]['MYVAR

我在使用variable-of-variable-way从playbook访问额外变量时遇到问题。 例如,我创建了一个包含以下内容的group_vars/mygroup.yml文件:

MYVAR=AAAA
然后我调用命令并传递额外的变量: ansible playbook-我测试playbook.yml--额外变量“MYVAR=BBB”

我需要从现有变量列表中获取MYVAR的实际值。我试着这样做:

- debug: var=hostvars[inventory_hostname]['MYVAR']
…我越来越

TASK: [test | debug var=AAA] *******************************
ok: [192.168.1.21] => {
    "var": {
        "AAA": "BBB"
    }
}

TASK: [test | debug var=hostvars[inventory_hostname]['AAA']] ***
ok: [192.168.1.21] => {
    "var": {
        "hostvars[inventory_hostname]['AAA']": "AAA"   // ← THIS IS THE PROBLEM
    }
}
如何获得从cli传递的AAA的实际值? 请不要对我说直接按名称使用AAA,因为这是更复杂逻辑的一部分,当我有一个注册变量列表时,我不能使用它们的名称

hostvars[inventory_hostname][**item**] ← variable of variable
先谢谢你

更新:也许Ansible已经支持类似的功能了

VARNAME: APP_ENV
APP_ENV: blabla
debug: var=${VARNAME} // blabla expected

更新2:问题说明。

命令行额外定义不属于hostvars

您可以通过
{{MYVAR}}

- debug: msg="{{ MYVAR }}"
TLDR
-debug:msg=“{AAA | default(hostvars[inventory_hostname]['AAA'])}”
定义变量时使用变量
AAA
,否则使用hostvar(groupvar)
AAA

#./main.yml
---
- hosts: all
  gather_facts: false
  tasks:
    - debug: msg="{{ foo }}"
    - debug: msg="{{ hostvars[inventory_hostname]['foo'] }}"
    - debug: msg="{{ foo|default(hostvars[inventory_hostname]['foo']) }}"

ansible playbook-i inventory-e“foo=whatever”main.yml

PLAY [all] ********************************************************************

TASK: [debug msg="{{ foo }}"] *************************************************
ok: [localhost] => {
    "msg": "whatever"
}

TASK: [debug msg="{{ hostvars[inventory_hostname]['foo'] }}"] *****************
ok: [localhost] => {
    "msg": "bar"
}

TASK: [debug msg="{{ foo|default(hostvars[inventory_hostname]['foo']) }}"] ****
ok: [localhost] => {
    "msg": "whatever"
}

PLAY RECAP ********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

现在,在ansible 2.0发布后,它已经消失了:


所以,就像是一个bug。

-debug:var=MYVAR
-debug:msg=“{{MYVAR}}”
谢谢,但所有变量都在列表中注册了。我只有列表中的{item}键:hostvars[inventory_hostname][item],我不能直接用名字来调用它。谢谢,但所有变量都注册在列表中。我只有列表中的{item}键:hostvars[inventory_hostname][item]@kivagant看起来这可能是1.9版本的bug。2.0按照您想要的方式运行(额外的变量被设置为hostvars)。我自己用2.0进行了测试。你的链接所代表的Bug看起来非常相似。特别是这个commit,但我有1.9.4,在我的例子中extravars不会覆盖hostvars。我将等待OSX软件包中的2.0 stable进行测试,但现在我需要找到一些不聪明的方法来解决这个错误。最终,发布了版本2,但目前Ansible 2有新的变量错误。您的剧本中产生此输出的确切代码是什么?对不起,代码来自2016年。我再也没有基础设施和剧本了。
#./group_vars/one
---
foo: "bar"
PLAY [all] ********************************************************************

TASK: [debug msg="{{ foo }}"] *************************************************
ok: [localhost] => {
    "msg": "whatever"
}

TASK: [debug msg="{{ hostvars[inventory_hostname]['foo'] }}"] *****************
ok: [localhost] => {
    "msg": "bar"
}

TASK: [debug msg="{{ foo|default(hostvars[inventory_hostname]['foo']) }}"] ****
ok: [localhost] => {
    "msg": "whatever"
}

PLAY RECAP ********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0
ansible-playbook -i localhost.ini test.yml --extra-vars="AAA=BBB"
TASK [debug]     *****************
ok: [localhost] => (item=AAA) => {
    "item": "AAA",
    "msg": "BBB"
}