2

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
}
user3086551
  • 405
  • 1
  • 4
  • 13
  • 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 May 26 '18 at 09:01
  • @jangroth I can avoid this whole approach if I'd be able to use dictionary in `vars` of a task. https://stackoverflow.com/questions/50477012/how-to-use-a-dictionary-of-registered-ansible-variables-in-vars – user3086551 May 30 '18 at 01:32

2 Answers2

2

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.

ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • When I use one more set_fact to assign it's value to some other variable, it doesn't look up its value from vars/main.yml Run the playbook with -vvv flag. `- set_fact: ultimate_variable: "{{ final_variable }}"` – user3086551 May 25 '18 at 22:51
  • understand now. updating the answer – ilias-sp May 25 '18 at 23:24
  • Is `vars` the collection of global variables? If yes, it may cause problems as the same variable may be defined on another host for some task. – user3086551 May 25 '18 at 23:33
  • 1
    `vars` is probably the highest level of global variable you can access, it contains even the `hostvars`. by "same variable may be defined on another host for some task", you mean if someone defines its own `vars`? you can define in main.yml your own `vars` variable, it will be in reality `vars.vars`, so i dont think it can lead to issues. try to print `vars` and you will see its structure. – ilias-sp May 25 '18 at 23:50
0

You could also skip the intermediary variable definition:

 - include_vars: "vars/main.yml"

  - set_fact:
      variable1: "var1"
      variable2: "var2"

  - set_fact: 
      ultimate_variable: "{{ lookup('vars', variable1 ~ '_' ~ variable2) }}"

  - debug:
      var: ultimate_variable
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62