Bootstrap FreeKB - Ansible - Getting Started with Modules
Ansible - Getting Started with Modules

Updated:   |  Ansible articles

Modules are used to do something on your managed nodes (e.g. the target systems), such as creating, removing or editing a file, installing or uninstalling packages, starting or stopping services, and so on.

Refer to run tasks on the control node if you want to run a module on your control node (your Ansible server).

In this example, the file module is used to create the /tmp/foo.txt file on your managed nodes.

---
- hosts: all
  tasks:
    - name: create /tmp/foo.txt
      file:
        path: /tmp/foo.txt
        state: touch
...

 


ansible-doc

The ansible-doc command can be used to show the Ansible documention on the archive module. For example, here is how you would view documentation on the copy module.

ansible-doc --type module copy

 


Collections

Modules are part of collections. For example, the file module is part of the ansible.builtin collection and the firewalld module is part of the ansible.posix collection. You can either use just the basename of the module, such as "file" and "firewalld", or you could also include the collection, which would be "ansible.builtin.file" and "ansible.posix.firewalld" in this example.

---
- hosts: all
  tasks:
    - name: create /tmp/foo.txt
      ansible.builtin.file:
        path: /tmp/foo.txt
        state: touch

    - name: Bind interface eth0 to the default zone
      ansible.posix.firewalld:
        interface: eth0
        state: enabled
...

 

Let's say the ansible-playbook command returns couldn't resolve module.

~]$ ansible-playbook testing.yml
ERROR! couldn't resolve module/action 'firewalld'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/jeremy.canfield/testing.yml': line 21, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


  - name: allow SSH in firewalld
    ^ here

 

This implies that a collection that contains the module (firewalld in this example) is not installed on your control node (your Ansible server). The ansible-galaxy install collection command can be used to install the collection. In this example, the ansible.posix collection is installed.

~]$ ansible-galaxy collection install ansible.posix
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'ansible.posix:1.2.0' to '/home/john.doe/.ansible/collections/ansible_collections/ansible/posix'
Downloading https://galaxy.ansible.com/download/ansible-posix-1.2.0.tar.gz to /home/john.doe/.ansible/tmp/ansible-local-15258q2alfwvq/tmpdesamf4k
ansible.posix (1.2.0) was installed successfully


 


Library

Library contains the directories that will be searched for modules. For example, if Ansible was installed on a Red Hat system using pip, library should contain the following directories. Refer to Ansible - Understanding Library for more on library.

/home/john.doe/.ansible/plugins/modules
/usr/share/ansible/plugins/modules
/usr/local/lib/python3.6/site-packages/ansible/modules

 


Parameters

Additional parameters can be included with modules. In this example, with the file module, the owner, group and mode parameters are included to set the owner, group, and mode of /tmp/foo.txt.

---
- hosts: all
  tasks:
    - name: create /tmp/foo.txt
      file:
        path: /tmp/foo.txt
        state: touch
        owner: john.doe
        group: admins
        mode: "0770"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e81028 in the box below so that we can be sure you are a human.