020 Get inventory aliases from notes

Extending example 016 Multiple inventory constructed.

Use case

Get the inventory aliases from the iocage property notes. In the inventory plugin vbotka.freebsd.iocage2, use the option inventory_hostname_tag to tell the plugin which tag to use.

Tree

shell> tree .
.
├── ansible.cfg
├── group_vars
│   └── all
│       └── swarms.yml
├── hosts
│   ├── 06_iocage.yml
│   └── 99_constructed.yml
├── host_vars
│   └── iocage_06
│       └── iocage.yml
├── iocage.ini
├── pb-iocage-swarms-create.yml
├── pb-iocage-swarms-destroy.yml
├── pb-test-all.yml
└── pb-test-db.yml

Synopsis

  • At a managed node:

    • create jails using a template and the option --count

    • at each jail, create property notes in the format tag1=val1 tag2=val2 ...

    • put the inventory alias into the tag alias=<alias>

  • In the inventory plugin vbotka.freebsd.iocage2, get the inventory aliases from the tag alias

  • In the inventory plugin ansible.builtin.constructed, create the inventory groups.

  • Display the jails and groups.

Requirements

Notes

  • The inventory files in the directory hosts are evaluated in alphabetical order.

Templates at iocage_06

[iocage_06]# iocage list -lt
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| JID  |          NAME           | BOOT | STATE |   TYPE   |     RELEASE     |          IP4          | IP6 | TEMPLATE | BASEJAIL |
+======+=========================+======+=======+==========+=================+=======================+=====+==========+==========+
| None | ansible-client          | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init            | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init-devel      | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-init-example    | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-nginx           | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-pkg-repo        | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-repos           | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-syslogng-client | off  | down  | template | 15.1-RELEASE-p1 | DHCP (not running)    | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+
| None | ansible-syslogng-server | off  | down  | template | 15.1-RELEASE-p1 | vnet0|172.16.99.41/24 | -   | -        | no       |
+------+-------------------------+------+-------+----------+-----------------+-----------------------+-----+----------+----------+

ansible.cfg

[defaults]
callback_result_format = yaml
deprecation_warnings = false
display_skipped_hosts = false
gathering = explicit
interpreter_python = auto_silent
log_path = /var/log/ansible.log

[connection]
pipelining = true

Inventory iocage.ini

iocage_06

[iocage]
iocage_06

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

group_vars

group_vars/all/swarms.yml
project: example_020
properties: "bpf=1 dhcp=1 vnet=1"

host_vars

host_vars/iocage_06/iocage.yml
names: [www_p1, www_p2, www_p3, www_d1, www_d2]
swarms:
  www_prod:
    count: 3
    template: ansible-client
  www_devel:
    count: 2
    template: ansible-client

Inventory hosts

hosts/06_iocage2.yml
plugin: vbotka.freebsd.iocage2
host: iocage_06
user: admin
cache: true
cache_plugin: ansible.builtin.jsonfile
cache_connection: /var/tmp/inventory_cache
cache_timeout: 3600
cache_prefix: iocage_06_
get_properties: true
inventory_hostname_tag: alias
hooks_results:
  - /var/db/dhclient-hook.address.epair0b
compose:
  ansible_host: (iocage_hooks.0 == '-') | ternary(iocage_ip4, iocage_hooks.0)
  iocage_tags: dict(iocage_properties.notes | regex_findall('(\w+)=([\w\-]+)'))
hosts/99_constructed.yml
plugin: ansible.builtin.constructed
keyed_groups:
  - prefix: vmm
    key: iocage_tags.vmm
  - prefix: project
    key: iocage_tags.project
  - prefix: swarm
    key: iocage_tags.swarm

Note

The value of the iocage tag alias is used as the inventory alias. If the iocage list is slow use the cache.

Playbook pb-iocage-swarms-create.yml

# This play is not idempotent! New jails are created each time this play runs.
- name: Create swarms (not idempotent!)
  hosts: iocage
  environment: "{{ iocage_env | d({}) }}"

  vars:

    debug: false

  tasks:

    - name: Create swarms.
      register: out_swarms
      ansible.builtin.command:
        cmd: >-
          iocage create
          --short
          --count {{ item.value.count }}
          --template {{ item.value.template }}
          {{ properties }}
      loop: "{{ swarms | dict2items }}"

    - name: Debug iocage create.
      when: debug | bool
      ansible.builtin.debug:
        var: out_swarms

    - name: Create tags.
      register: out_tags
      vars:
        jail_name: "{{ item.1 | split | first }}"
        iocage_tags: >-
          vmm={{ inventory_hostname}}
          project={{ project }}
          swarm={{ item.0.item.key }}
          alias={{ names[idx] }}
      ansible.builtin.command:
        cmd: >-
          iocage set
          notes="{{ iocage_tags }}"
          {{ jail_name }}
      loop: "{{ out_swarms.results | subelements('stdout_lines') }}"
      loop_control:
        index_var: idx
        label: "{{ jail_name }} {{ iocage_tags }}"

    - name: Debug create tags.
      when: debug | bool
      ansible.builtin.debug:
        var: out_tags

Playbook output - Create swarms

(env) > ansible-playbook -i iocage.ini pb-iocage-swarms-create.yml
PLAY [Create swarms (not idempotent!)] *****************************************

TASK [Create swarms.] **********************************************************
changed: [iocage_06] => (item={'key': 'www_prod', 'value': {'count': 3, 'template': 'ansible-client'}})
changed: [iocage_06] => (item={'key': 'www_devel', 'value': {'count': 2, 'template': 'ansible-client'}})

TASK [Create tags.] ************************************************************
changed: [iocage_06] => (item=8889f44b vmm=iocage_06 project=example_020 swarm=www_prod alias=www_p1)
changed: [iocage_06] => (item=3dac9367 vmm=iocage_06 project=example_020 swarm=www_prod alias=www_p2)
changed: [iocage_06] => (item=07d85050 vmm=iocage_06 project=example_020 swarm=www_prod alias=www_p3)
changed: [iocage_06] => (item=dc039f0c vmm=iocage_06 project=example_020 swarm=www_devel alias=www_d1)
changed: [iocage_06] => (item=2836ab94 vmm=iocage_06 project=example_020 swarm=www_devel alias=www_d2)

PLAY RECAP *********************************************************************
iocage_06                  : ok=2    changed=2    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

Hint

Run the below command to see the complete inventory

shell> ansible-inventory -i hosts --list --yaml

Jails at iocage_06

[iocage_06]# iocage list -l
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| JID  |   NAME   | BOOT | STATE | TYPE |     RELEASE     |          IP4          | IP6 |    TEMPLATE    | BASEJAIL |
+======+==========+======+=======+======+=================+=======================+=====+================+==========+
| None | 07d85050 | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| None | 2836ab94 | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| None | 3dac9367 | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| None | 8889f44b | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| None | dc039f0c | off  | down  | jail | 15.1-RELEASE-p1 | DHCP (not running)    | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 9    | test-151 | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.189 | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 10   | test-152 | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.188 | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+
| 8    | test-153 | off  | up    | jail | 15.1-RELEASE-p1 | epair0b|172.16.99.187 | -   | ansible-client | no       |
+------+----------+------+-------+------+-----------------+-----------------------+-----+----------------+----------+

Playbook pb-test-all.yml

- name: Display all groups.
  hosts: all

  tasks:

    - debug:
        msg: |
          {{ iocage_properties.host_hostuuid | d('UNDEFINED') }}
          {{ iocage_tags | d({}) | to_yaml }}

    - debug:
        msg: |
          {% for group in groups %}
          {{ group }}: {{ groups[group] }}
          {% endfor %}
      run_once: true

Playbook output - All groups

(env) > ansible-playbook -i hosts pb-test-all.yml
PLAY [Display all groups.] *****************************************************

TASK [debug] *******************************************************************
ok: [test_153] => 
    msg: |-
        test-153
        {alias: test_153, vmm: iocage_06}
ok: [www_p1] => 
    msg: |-
        8889f44b
        {alias: www_p1, project: example_020, swarm: www_prod, vmm: iocage_06}
ok: [www_d2] => 
    msg: |-
        2836ab94
        {alias: www_d2, project: example_020, swarm: www_devel, vmm: iocage_06}
ok: [www_p2] => 
    msg: |-
        3dac9367
        {alias: www_p2, project: example_020, swarm: www_prod, vmm: iocage_06}
ok: [test_152] => 
    msg: |-
        test-152
        {alias: test_152, vmm: iocage_06}
ok: [www_d1] => 
    msg: |-
        dc039f0c
        {alias: www_d1, project: example_020, swarm: www_devel, vmm: iocage_06}
ok: [www_p3] => 
    msg: |-
        07d85050
        {alias: www_p3, project: example_020, swarm: www_prod, vmm: iocage_06}
ok: [test_151] => 
    msg: |-
        test-151
        {alias: test_151, vmm: iocage_06}

TASK [debug] *******************************************************************
ok: [test_153] => 
    msg: |-
        all: ['test_153', 'www_p1', 'www_d2', 'www_p2', 'www_d1', 'test_152', 'www_p3', 'test_151']
        ungrouped: []
        vmm_iocage_06: ['test_153', 'www_p1', 'www_d2', 'www_p2', 'www_d1', 'test_152', 'www_p3', 'test_151']
        project_example_020: ['www_p1', 'www_d2', 'www_p2', 'www_d1', 'www_p3']
        swarm_www_prod: ['www_p1', 'www_p2', 'www_p3']
        swarm_www_devel: ['www_d2', 'www_d1']

PLAY RECAP *********************************************************************
test_151                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_152                   : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test_153                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
www_d1                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
www_d2                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
www_p1                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
www_p2                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
www_p3                     : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Playbook pb-iocage-swarms-destroy.yml

- name: Destroy swarms.
  hosts: iocage
  environment: "{{ iocage_env | d({}) }}"

  vars:

    debug: false

  tasks:

    - name: Stop and destroy swarms.
      vars:
        group: "swarm_{{ item }}"
        uuids: "{{ groups[group] | map('extract', hostvars, ['iocage_properties', 'host_hostuuid']) }}"
      block:

        - name: Stop swarms.
          register: out
          ansible.builtin.command: "iocage stop {{ uuids | join(' ') }}"
          loop: "{{ swarms.keys() }}"

        - name: Debug stop swarms.
          when: debug | bool
          ansible.builtin.debug:
            var: out

        - name: Destroy swarms.
          register: out
          ansible.builtin.command: "iocage destroy -f {{ uuids | join(' ') }}"
          loop: "{{ swarms.keys() }}"

        - name: Debug stop swarms.
          when: debug | bool
          ansible.builtin.debug:
            var: out

Playbook output - Destroy swarms

Destroy the swarms if you don’t need them any more.

(env) > ansible-playbook pb-iocage-swarms-destroy.yml -i iocage.ini -i hosts
PLAY [Destroy swarms.] *********************************************************

TASK [Stop swarms.] ************************************************************
changed: [iocage_04] => (item=db)
changed: [iocage_02] => (item=www_prod)
changed: [iocage_02] => (item=www_devel)

TASK [Destroy swarms.] *********************************************************
changed: [iocage_04] => (item=db)
changed: [iocage_02] => (item=www_prod)
changed: [iocage_02] => (item=www_devel)

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