Search code examples
ansibleansible-inventoryansible-facts

How to access a programmatically constructed ansible variable?


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
}

Solution

  • 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.