0

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 ?

JG801
  • 121
  • 2
  • 7

1 Answers1

0

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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • I meant the module foo.bar.install to be a Python module. So I need programmatically from there to call a tasklist or playbook. The final result should be one simple install task in a playbook, which calls the Python module, which runs the specialized task list or playbook. – JG801 Jun 24 '22 at 07:30
  • A task can't call a playbook. Instead of a hypothetical *foo.bar.install* module, which must be used in a playbook anyway, run the *foo.bar.install* playbook. If this doesn't solve the use case [edit] the question and make the case [mre]. – Vladimir Botka Jun 24 '22 at 08:12