0

I'm not sure that this is possible.

I want to define a var at run-time and use it to access another var(defined in file,playbook..).

  1. defined at run-time :

    typeConfig (possible values: "in_config" or "out_config")
    
  2. defined in playbook:

    in_config:
      url_config: http://localhost/configuration
    
    out_config:
      url_config: http://config.pi.dyn-dns.org/configuration
    

I need to resolve something similar to this:

{{ {{ typeConfig }}.url_config }}

I try with:

- name: Mytest
  hosts: all
  gather_facts: false
  sudo: yes
  vars:
    - in_config:
         url_config: http://localhost/configuration
    - out_config:
         url_config: http://config.pi.dyn-dns.org/configuration
  tasks:
     - set_fact:
          typeConfig: in_config
     - name: Value in_config.url_config
       debug: msg=" {{in_config.url_config}}"

     - name: Value out_config.url_config
       debug: msg=" {{out_config.url_config}}"

     - name: Value typeConfig
       debug: var=typeConfig

     - debug: msg="{{ {{ typeConfig }}.url_config }} "

ACTUAL RESULTS

task path: /home/nor/gitrepos/iiot-iac/ansible/myUnitTest.yml:19 fatal: [node1]: FAILED! => { "failed": true, "msg": "template error while templating string: expected token ':', got '}'. String: {{ {{ typeConfig }}.url_config }} " } " }

Nabidul
  • 16
  • 3

1 Answers1

1

You can access the value using:

- debug:
    msg: "{{ vars[typeConfig].url_config }}"

Remember that {{ ... }} is not a way to write a variable name, but to start a Jinja2 expression. And when querying values, variables are referenced using Jinja2 expressions in Ansible, thus using {{ {{ ... }} }} makes no sense.

techraf
  • 64,883
  • 27
  • 193
  • 198