Posted on Leave a comment

How to create a hello world Ansible module with Documentation

If you cannot locate a plugin that suits your needs by using Ansible, you can easily extend the default functionality by creating your own python module. In this article I will explain the procedure for the creation of a module.

First of all you should create a python code and use Ansible SDK. A detailed description for the creation of the development environment can be found on official documentation.

The example module is a hello world, that gets as an input your name, surname and prints a hello message.

Copy your hello.py on ansible modules location. On my working machine this is the path /usr/local/lib/python3.9/site-packages/ansible/modules

When you include your documentation on the python file, you can explore it with:

ansible-doc hello
Documentation for module

If you try a module run without the required parameters, it will fail as shown below:

Plugin run without required parameters

Run your custom module by using the below playbook:

Input with only name as parameter:

---
- name: test playbook using custom code
  hosts: localhost
  tasks:
    - name: using my custom module
      hello:
        name: Gerasimos
      register: result
      
    - name: show output
      debug:
        var: result

Input with name and surname as parameters:

---
- name: test playbook using custom code
  hosts: localhost
  tasks:
    - name: using my custom module
      hello:
        name: Gerasimos
        surname: Alexiou
      register: result
      
    - name: show output
      debug:
        var: result

Code for the example module can be found on my github repo.

Posted on Leave a comment

Ansible loop over nested dictionary subelements – list object has no attribute

Sometimes it could be tricky in Ansible to loop over a nested key-value list. Take for example the below dictionary which includes a nested list of disks. The upper element of the .yml file is vms which includes name, folder, cpus, sockets, memory and disk.

The disk element consists of disksize and disktype. This .yml file has been created on a previous post which explains how to automatically provision VMware servers.

 vms:
 name: test1-ansible
 folder: ansible
 cpus: 1
 sockets: 1
 memory: 64
 disk:
  - disksize: 64
    disktype: thin
  - disksize: 100
    disktype: thin 

If you try to loop over this list you will probably get an error like list object has no attribute.

With the subelements command you can loop over your main list (vms) and access your nested one (disk) with a different index.

In order to retrieve the vm name you should use item.0. The nested values are placed under item.1

---
- name: test playbook 
  hosts: localhost 
  vars_files: vms.yml
  tasks:
    - name: loop over nested
      debug:
        msg: '"{{ item.1.disksize }}"  "{{ item.1.disktype }}"'
      loop: "{{ vms| subelements('disk') }}" 

By performing a debug print, we can successfully get the nested keys values.