Is it possible to run a tasklist from within an Ansible module?

117 views Asked by At

I am developing a collection containing a number of playbooks for complex system configurations. However, I'd like to simplify the usage of them to a single module plugin, named 'install'. In the Python code of the module will be decided what playbook will be called to perform the actual task. Say

- name: Some playbook written by the end user
  hosts: all
  tasks:
    ...
    - install:
        name: docker
        version: latest
        state: present
    ...

Based on the specified name, version and state will the Python code of the install module invoke programmatically the adequate playbook(s) to perform the installation of the latest release of Docker. These playbooks are also included into the collection.

If task lists are a better fit than playbooks, then task lists it is.

In fact, I don't mind the precise implementation. For as long as:

  1. Everything is packed into a collection
  2. The end user does it all with one 'install' task as depicted above

Is that even possible ?

1

There are 1 answers

2
Vladimir Botka On

Yes. It's possible. For example, given the variable in a file

shell> cat foo_bar_install.yml
foo_bar_install:
  name: docker
  version: latest
  state: present

Create the playbooks in the collection foo.bar

shell> tree collections/ansible_collections/foo/bar/
collections/ansible_collections/foo/bar/
├── galaxy.yml
└── playbooks
    ├── install_docker.yml
    └── install.yml

1 directory, 3 files
shell> cat collections/ansible_collections/foo/bar/playbooks/install_docker.yml
- name: Install docker
  hosts: all
  gather_facts: false
  tasks:
    - debug:
        msg: |
          Playbook foo.bar.install_docker.yml
          version: {{ version|d('UNDEF') }}
          state: {{ state|d('UNDEF') }}
shell> cat collections/ansible_collections/foo/bar/playbooks/install.yml 
- import_playbook: "foo.bar.install_{{ foo_bar_install.name }}.yml"
  vars:
    version: "{{ foo_bar_install.version }}"
    state: "{{ foo_bar_install.state }}"

Given the inventory

shell> cat hosts 
host1
host2

Run the playbook foo.bar.install.yml and provide the extra variables in the file foo_bar_install.yml

shell> ansible-playbook foo.bar.install.yml -e @foo_bar_install.yml

PLAY [Install docker] ************************************************************************

TASK [debug] *********************************************************************************
ok: [host1] => 
  msg: |-
    Playbook foo.bar.install_docker.yml
    version: latest
    state: present
ok: [host2] => 
  msg: |-
    Playbook foo.bar.install_docker.yml
    version: latest
    state: present

PLAY RECAP ***********************************************************************************
host1: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0