1
votes

I have constructed an ansible variable using two other defined ansible variables. The constructed variable is defined in the vars/main.yml and I want to access the defined value in vars/main.yml. vars/main.yml

---
var1_var2: "some value"

Now, I construct the variable

---
- 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 }}"

If I run the playbook with -vvv flag, I can see that ultimate_variable sets to var1_var2 while I want to get the value defined in the vars/main.yml i.e., 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
Even if this is technically possible, be aware that you are creating code that is hard to read, test and maintain! Are you sure your problem is not better solved with a standard dict / list / collection?Jan Groth
@jangroth I can avoid this whole approach if I'd be able to use dictionary in vars of a task. stackoverflow.com/questions/50477012/…user3086551

1 Answers

1
votes

updated answer:

use the lookup plugin to do the double replacement:

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

playbook:

  - 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

output:

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

hope it helps.