411 Include variables from nested directories

Use case

Create a dictionary of variables from nested directories in al_vars. Use the role vbotka.freebsd.lib tasks al_include_dir_vars.yml

- name: Create dict of vars from nested directories in al_vars
  vars:
    al_include_dir_vars_dir: "{{ playbook_dir }}/al_vars"
  include_role:
    name: vbotka.freebsd.lib
    tasks_from: al_include_dir_vars

Tree

shell > tree .
.
├── al_vars
│   └── team
│       ├── devel
│       ├── production
│       └── qa
├── ansible.cfg
├── example.rst
├── hosts
├── out
│   └── out-01.txt
└── pb.yml

Synopsis

  • At the managed node:

    • Create a dictionary of variables from nested directories in the controller’s directory al_vars

    • Display the created dictionary.

Requirements

Notes

TBD

Note

vbotka.freebsd.lib is the role lib in the collection vbotka.freebsd
vbotka.ansible_lib is the role ansible_lib in the namespace vbotka
Please make sure the versions are the same before you switch between them.

ansible.cfg

[defaults]
inventory = $PWD/hosts
gathering = explicit
callback_result_format = yaml
display_skipped_hosts = false

[connection]
pipelining = true

Inventory hosts

iocage_04 ansible_host=10.1.0.29

[iocage]
iocage_04

[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent

al_vars

al_vars/team/devel
- team: devel
  users: [charlie, david]
al_vars/team/production
- team: production
  users: [alice, bob]
al_vars/team/qa
- team: qa1
  users: [mallory, ted]
- team: qa2
  users: [darth, wendy]

Expected results

al_vars:
  team:
    devel:
      - team: devel
        users: [charlie, david]
    production:
      - team: production
        users: [alice, bob]
    qa:
      - team: qa1
        users: [mallory, ted]
      - team: qa2
        users: [darth, wendy]

Playbook pb.yml

- name: Test vbotka.freebsd.lib al_include_dir_vars
  hosts: all

  tasks:

    - name: Create dict of vars from nested directories al_vars
      vars:
        al_include_dir_vars_dir: "{{ playbook_dir }}/al_vars"
      include_role:
        name: vbotka.freebsd.lib
        tasks_from: al_include_dir_vars

    - debug:
        msg: |
          al_vars:
            {{ al_vars | to_yaml(indent=2) | indent(2) }}

Playbook output - Display variables

(env) > ansible-playbook pb.yml
PLAY [Test vbotka.freebsd.lib al_include_dir_vars] *****************************

TASK [Create dict of vars from nested directories al_vars] *********************
included: vbotka.freebsd.lib for iocage_04

TASK [vbotka.freebsd.lib : Al_include_dir_vars: Get list of directories.] ******
ok: [iocage_04 -> localhost]

TASK [vbotka.freebsd.lib : Al_include_dir_vars: Set list of directories.] ******
ok: [iocage_04]

TASK [vbotka.freebsd.lib : Al_include_dir_vars: Set dictionary of lists of files.] ***
ok: [iocage_04] => (item=team)

TASK [vbotka.freebsd.lib : Al_include_dir_vars: Get content of files.] *********
ok: [iocage_04] => (item=[{'key': 'team', 'value': ['devel', 'production', 'qa']}, 'devel'])
ok: [iocage_04] => (item=[{'key': 'team', 'value': ['devel', 'production', 'qa']}, 'production'])
ok: [iocage_04] => (item=[{'key': 'team', 'value': ['devel', 'production', 'qa']}, 'qa'])

TASK [vbotka.freebsd.lib : Al_include_dir_vars: Create dictionary al_vars.] ****
ok: [iocage_04] => (item=team)

TASK [debug] *******************************************************************
ok: [iocage_04] => 
    msg: |-
        al_vars:
          team:
            devel:
            - team: devel
              users: [charlie, david]
            production:
            - team: production
              users: [alice, bob]
            qa:
            - team: qa1
              users: [mallory, ted]
            - team: qa2
              users: [darth, wendy]

PLAY RECAP *********************************************************************
iocage_04                  : ok=7    changed=0    unreachable=0    failed=0    skipped=7    rescued=0    ignored=0