Ansible nested definition and reference issues

Viewed 278

I have a problem with implementing Ansible nested reference YAML variable file.

Example of user dictionary file variable:

  1. User dictionary file variable, dev_infra_bastion_0_example_com is node FQDN name

    # user varialbe file
    root@dev-infra-bastion-0:~/ansible-stats# hostname
    dev-infra-bastion-0.example.com
    

    vars/user.yml

    dev_infra_bastion_0_example_com:
      admin:
        - admin1
        - admin2
        - admin3
      people:
        - peopl1
        - peopl2
        - peopl3
    
  2. I want to pass the dictionary defined in the vars/user.yml file after parsing through this playbook

    ## create hosts user
    - hosts: test1
      become: yes
      vars_files:
        - vars/user.yml
    
      pre_tasks:
      - name: debug
        debug:
          msg: "{{ ansible_nodename.replace('-','_').replace('.','_') }}"
        register: mess
    
      - name: debug mess
        debug:
          msg: "{{ mess }}"
    
  3. Run info

    PLAY [test1] *******************************************************************************************************************************************************************************************************
    
    TASK [debug] *******************************************************************************************************************************************************************************************************
    ok: [10.1.204.100] => {
        "msg": "dev_infra_bastion_0_example_com"
    }
    
    TASK [debug mess] **************************************************************************************************************************************************************************************************
    ok: [10.1.204.100] => {
        "msg": {
            "changed": false,
            "failed": false,
            "msg": "dev_infra_bastion_0_example_com"       #Here,Should be a dictionary,but it's string
        }
    }
    
    PLAY RECAP *********************************************************************************************************************************************************************************************************
    10.1.204.100               : ok=2    changed=0    unreachable=0    failed=0
    
    debug ------------------------------------------------------------------- 0.10s
    debug mess -------------------------------------------------------------- 0.06s
    
1 Answers

There is two to three things wrong in your playbook.

  1. Do not use a debug and register if you want to register a variable, use the appropriate set_fact module

    - set_fact:
        mess: "{{ ansible_nodename.replace('-','_').replace('.','_') }}"
    
  2. If you want to access a variable from a variable, you need to either use the vars lookup.

    - debug:
        var: lookup('vars', mess)
    
  3. Bonus: to replace multiple characters by an underscore, maybe a regex_replace could be nicer, so, you can really do it in a single task:

    - debug:
        var: lookup('vars', ansible_nodename | regex_replace('[\.-]', '_'))
    

An example playbook would be:

- hosts: localhost
  gather_facts: no

  tasks:
    - debug:
        var: lookup('vars', ansible_nodename | regex_replace('[\.-]', '_'))
      vars:
        ansible_nodename: dev-infra-bastion-0.example.com
        dev_infra_bastion_0_example_com:
          admin:
            - admin1
            - admin2
            - admin3
          people:
            - peopl1
            - peopl2
            - peopl3

Which would yield the recap:

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

TASK [debug] ******************************************************************************************************
ok: [localhost] => 
  lookup('vars', ansible_nodename | regex_replace('[\.-]', '_')):
    admin:
    - admin1
    - admin2
    - admin3
    people:
    - peopl1
    - peopl2
    - peopl3

PLAY RECAP ********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Related