DEV Community

Cover image for Exploring Ansible: Ad-Hoc Commands, Modules, and Playbooks
Sushant Gaurav
Sushant Gaurav

Posted on • Updated on

Exploring Ansible: Ad-Hoc Commands, Modules, and Playbooks

Exploring Ansible: Ad-Hoc Commands and Modules

Ansible is a powerful open-source automation tool used for configuration management, application deployment, and task automation. It offers several methods to manage and control your infrastructure, and in this article, we'll dive into two fundamental approaches: Ad-Hoc Commands and Modules.

Three Ways to Manage Infrastructure

Before we get into the details, let's understand the three primary ways you can manage your infrastructure using Ansible:
1. Ad-Hoc Commands
2. Modules
3. Playbooks

Let's learn about these methods in detail with examples.

1. Ad-Hoc Commands

Ad-Hoc commands are the quickest way to execute simple Linux commands on one or more managed nodes. They are ideal for tasks that don't require idempotency, meaning they will always run the specified command, even if the state of the node hasn't changed. Ad-Hoc commands are perfect for one-time tasks, such as checking server uptime or restarting a service.

Syntax

Ad-Hoc commands are executed using the -a option, and you provide the Linux command you want to run after -a. The syntax is as follows:

ansible <group-name> -a "<linux-command>"
Enter fullscreen mode Exit fullscreen mode

How to Use Ad-Hoc Commands

Let's look at an example:

ansible all -a "ls"
Enter fullscreen mode Exit fullscreen mode

This command will list the files on all the managed nodes. Ad-Hoc commands can also be used for installing packages and other tasks, but they are not always recommended for complex operations.

If you need to execute commands with root privileges, you can use the -b option, which stands for "become". Here's an example:

ansible <group-name> -ba "yum install <package-name> -y"
Enter fullscreen mode Exit fullscreen mode

Please note that the -b option is essential for privilege escalation if you need root access.

2. Modules

Modules in Ansible provide a more powerful and flexible approach to automation. They use reusable YAML scripts to run single tasks on managed nodes. Unlike Ad-Hoc commands, modules are idempotent, which means they ensure that the desired state is achieved, and they only make changes if necessary. Ansible ships with a variety of modules, collectively referred to as the module library, that can be executed directly on remote hosts or through playbooks.

Syntax

To use modules, you specify the module to use with the -m option, followed by the module name, and then provide the required parameters as a YAML script. The syntax is as follows:

ansible <group-name> -m <module-name> -a "<YAML-script>"
Enter fullscreen mode Exit fullscreen mode

How to Use Modules

Modules are highly versatile and can be used for a wide range of tasks. For example, you can use the yum module to install packages. Here's how you might install a package using the yum module:

ansible -b -m yum -a "name=<package-name> state=present"
Enter fullscreen mode Exit fullscreen mode

Some essential modules in Ansible include copy, service, setup, and many more, allowing you to manage various aspects of your servers.

The setup module is particularly useful as it automatically fetches the current configuration of managed nodes. You can use it to gather information about the machine, and you can filter the results for specific details. For example, to get IPv4 addresses, you can use the following command:

ansible <group-name> -m setup -a "filter=ansible_all_ipv4_addresses"
Enter fullscreen mode Exit fullscreen mode

3. Playbooks

Playbooks are written in YAML format and offer a more structured and comprehensive approach to infrastructure management. A playbook typically includes variables, tasks, handlers, and roles. Each playbook consists of a list of modules and has a .yml file extension.

A playbook can be divided into several sections, including:

  • Target Section: Where you define the target hosts.
  • Task Section: Where you define a list of modules to run in an ordered manner.
  • Variable Section: Where you define the variables used in the tasks.

Important Points

  • To write a playbook, you use the YAML language.
  • The playbook begins with --- to mark the start and can optionally end with ....
  • YAML syntax requires consistent indentation.
  • To run a playbook, use the ansible-playbook command:
ansible-playbook <playbook-name.yml>
Enter fullscreen mode Exit fullscreen mode

Examples

  1. To gather information about the managed nodes without performing any specific tasks, you can use the target section:

    --- # Gathering information
    - hosts: <group-name>
    user: <user-name>
    become: yes
    connection: ssh
    gather_fact: yes
    
  2. To perform tasks on the managed nodes, you define them in the task section. You can specify a task name (optional) and use the action key to define the actual task:

    --- # Installing httpd
    - hosts: <group-name>
    user: <user-name>
    become: yes
    connection: ssh
    tasks:
        - name: install httpd
        yum:
            name: httpd
            state: installed
    
  3. You can use variables for flexibility by defining them in the variable section and then using them in your tasks:

    --- # Installing httpd using a variable
    - hosts: <group-name>
    user: <user-name>
    become: yes
    connection: ssh
    vars:
        packageName: httpd
    tasks:
        - name: install httpd
        yum:
            name: '{{packageName}}'
            state: installed
    
  4. Handlers are similar to tasks but are dependent on other tasks. If a task is completed, it notifies the system to execute the handler:

    --- # Handlers
    - hosts: <group-name>
    user: <user-name>
    become: yes
    connection: ssh
    tasks:
        - name: install httpd
        yum:
            name: httpd
            state: installed
        notify: restart HTTPD
    handlers:
        - name: restart HTTPD
        service:
            name: httpd
            state: restarted
    
  5. You can use loops to iterate over a list of items, such as creating multiple users:

    --- # Loops
    - hosts: <group-name>
    user: <user-name>
    become: yes
    connection: ssh
    tasks:
        - name: adding a list of users
        user:
            name: '{{item}}'
            state: present
        loop:
            - username1
            - username2
            - username3
            - username4
            - username5
    

Note:
Please ensure to replace <group-name> and <user-name> with your specific values in the provided examples. These methods provide a comprehensive approach to managing your infrastructure using Ansible.

Conclusion

Whether you need to perform simple, one-time tasks or manage complex configurations, Ansible provides three fundamental approaches: Ad-hoc commands, Modules, and Playbooks.

Ad-hoc commands offer a quick and easy way to execute Linux commands on one or more managed nodes. They are perfect for tasks that don't require idempotence, making them suitable for tasks like checking server uptime and restarting services.

Modules, on the other hand, provide a more structured and idempotent approach to managing infrastructure. Ansible's extensive library of modules covers a wide range of tasks, including package installation, user management, and service configuration. This approach is well-suited for complex configurations and automation.

Finally, Playbooks bring everything together in a structured YAML format. Playbooks are ideal for orchestrating multiple tasks, defining variables, and handling dependencies between different tasks. They are the heart of Ansible's automation capabilities, offering the power and flexibility needed for advanced infrastructure management.

Remember that the choice between these methods depends on your specific requirements. Ad-hoc commands are great for quick and simple tasks, while modules and playbooks are more suitable for complex automation and configuration management. By understanding and mastering these Ansible techniques, you can take your infrastructure automation to the next level, ensuring efficient, reliable, and consistent management of your IT resources.


To learn more about Ansible, please click here

Top comments (0)