jm1.ansible.execute_module (2023.2.22) — module

Execute an Ansible module

Authors: Jakob Meng (@jm1)

preview | supported by community

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install jm1.ansible:==2023.2.22


Add to requirements.yml

  collections:
    - name: jm1.ansible
      version: 2023.2.22

Description

Execute an Ansible module whose I(name) and I(args) options can be expanded from Jinja2 templates.

Results of the module call will be passed back to the caller.

Ansible offers several modules to dynamically and statically load tasks from files, e.g. with M(ansible.builtin.include_tasks) and M(ansible.builtin.import_tasks). But Ansible does not offer any module to load tasks from variables without this indirection through files.

Ansible's C(block) statement does not support Jinja2 templates, so Ansible does not allow to define tasks in a variable and pass this variable to C(block).

The implementation is inspired by Ansible's M(normal) action plugin which unfortunately cannot be called directly: U(https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/normal.py)

The C(when) support is inspired by Ansible's M(assert) action plugin: U(https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/action/assert.py)

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Define a module call
  set_fact:
    slurp_proc_mounts:
      name: ansible.builtin.slurp
      args:
        src: /proc/mounts
      register: slurp_output
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run a module defined in a variable
  jm1.ansible.execute_module: '{{ slurp_proc_mounts }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Print registered variable
  debug:
    var: slurp_output

Inputs

    
args:
    description:
    - Arguments which will be passed to the module.
    type: dict

name:
    description:
    - Name of module to execute.
    - Both fully qualified collection names (FQCN) such as M(ansible.builtin.copy) and
      short names such as M(copy) are supported.
    required: true
    type: str

when:
    description:
    - Basic conditional, similar to Ansible's C(when) keyword, which must evaluate to
      C(True) for Ansible to execute this module.
    type: list

register:
    description:
    - Name of a registered variable to create which contains the output of the module.
    type: str

See also