2

Imagine I have a YAML configuration file config.yml like the following:

---
vendor:
  url: https://acme.com

According to Ansible documentation, I can load this data structure into a variable:

- name: Include vars of stuff.yaml into the 'stuff' variable (2.2).
  ansible.builtin.include_vars:
    file: {{ lookup('ansible.builtin.env', 'STUFF') }}
    name: stuff

But how to use this variable and its subsidiaries? How to access the stuff/vendor/url variable properly, and is there a way to print out the whole variable tree?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Open Food Broker
  • 1,095
  • 1
  • 8
  • 31

1 Answers1

0

To use those variables, you have to use the templating system of Ansible, which is Jinja.

Since your variable is currently an URL, here would be an example usage:

- ansible.builtin.get_url:
    url: "{{ stuff.vendor.url }}"
    dest: /tmp/vendor_page_copy

To see the content of the variable, you can:

  1. use the debug module with the var parameter:
    - ansible.builtin.debug:
        var: stuff.vendor.url
    
    which outputs something like
    ok: [localhost] => 
      stuff.vendor.url: https://acme.com
    
    or
    - ansible.builtin.debug:
        var: stuff
    
    which outputs something like
    ok: [localhost] => 
      stuff:
        vendor:
          url: https://acme.com
    
  2. use the debug module with the msg parameter, and template the variable:
    - ansible.builtin.debug:
        msg: "{{ stuff.vendor.url }}"
    
    which outputs something like
    ok: [localhost] => 
      msg: https://acme.com
    
    The same can be done with msg: "{{ stuff }}", as seen above.
  3. run your playbook with the -v option:
    ansible-playbook play.yml -v
    
    which outputs something like
    ok: [localhost] => changed=false 
      ansible_facts:
        stuff:
          vendor:
            url: https://acme.com
      ansible_included_var_files:
      - /usr/local/ansible/stuff.yml
    

Note: your own output would seem really different that was is displayed here, and be in JSON, in a hard to read one liner. If this is the case, you would greatly benefit from using the YAML callback.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83