如何访问以编程方式构造的ansible变量?

时间:2018-05-25 21:56:48

标签: ansible ansible-inventory ansible-facts

我使用另外两个定义的ansible变量构造了一个ansible变量。构造的变量在vars / main.yml中定义,我想访问vars / main.yml中定义的值。 VARS / main.yml

---
var1_var2: "some value"

现在,我构建变量

---
- name: Construct and get the value
  hosts: localhost
  tasks:
  - include_vars: "vars/main.yml"
  - set_fact:
      variable1: "var1"
      variable2: "var2"
  - set_fact:
      final_variable: "{{ variable1 }}_{{ variable2 }}"

  - set_fact: 
      ultimate_variable: "{{ final_variable }}"

如果我使用-vvv标志运行playbook,我可以看到ultimate_variable设置为var1_var2,而我想获取vars / main.yml中定义的值  即,some value

TASK [set_fact] 

ok: [localhost] => {
"ansible_facts": {
    "variable1": "var1",
    "variable2": "var2"
},
"changed": false,
"failed": false
}

TASK [set_fact] task path: /home/ubuntu/test.yml:78

ok: [localhost] => {
"ansible_facts": {
    "final_variable": "var1_var2"
},
"changed": false,
"failed": false
}

TASK [set_fact] 

ok: [localhost] => {
"ansible_facts": {
    "ultimate_variable": "var1_var2"
},
"changed": false,
"failed": false
}

1 个答案:

答案 0 :(得分:1)

更新回答:

使用查找插件进行双重替换:

ultimate_variable: "{{ lookup('vars', '{{final_variable}}') }}"

剧本:

  - include_vars: "vars/main.yml"

  - set_fact:
      variable1: "var1"
      variable2: "var2"
  - set_fact:
      final_variable: "{{ variable1 }}_{{ variable2 }}"

  - set_fact: 
      ultimate_variable: "{{ lookup('vars', '{{final_variable}}') }}"

  - debug:
      var: ultimate_variable

输出:

PLAY [localhost] ****************************************************************************************************************************************************************************************************

TASK [include_vars] *************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] *****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] *****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [set_fact] *****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ultimate_variable": "some value"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0
希望它有所帮助。