How to use the command "ansible" (with examples)

How to use the command "ansible" (with examples)

Ansible is a powerful open-source automation tool that allows you to manage groups of computers remotely over SSH. It simplifies the process of automating tasks such as software deployments, configuration management, and infrastructure orchestration. This article will provide examples of various use cases for the “ansible” command, showcasing its versatility and usefulness.

Use case 1: List hosts belonging to a group

Code:

ansible group --list-hosts

Motivation: By listing the hosts belonging to a specific group, you can easily check which hosts are part of that group and verify the inventory configuration.

Explanation:

  • ansible: The main command.
  • group: The name of the group whose hosts you want to list.
  • --list-hosts: An option that instructs Ansible to display the hosts belonging to the specified group.

Example output:

hosts (2):
    host1
    host2

Use case 2: Ping a group of hosts

Code:

ansible group -m ping

Motivation: Pinging a group of hosts allows you to check if they are online and accessible, ensuring the basic connectivity before running any complex tasks.

Explanation:

  • ansible: The main command.
  • group: The name of the group whose hosts you want to ping.
  • -m ping: Specifies the module to invoke. In this case, the ping module is used to ping the hosts.

Example output:

host1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
host2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Use case 3: Display facts about a group of hosts

Code:

ansible group -m setup

Motivation: Gathering facts about the hosts in a group allows you to obtain detailed information about their system configuration, hardware, and network interfaces, which can be useful for debugging or preparing the environment for specific tasks.

Explanation:

  • ansible: The main command.
  • group: The name of the group whose hosts you want to gather facts from.
  • -m setup: Tells Ansible to invoke the setup module, which collects facts about the hosts.

Example output:

host1 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64",
        "ansible_distribution": "Ubuntu",
        "ansible_distribution_release": "18.04",
        ...
    },
    "changed": false
}
host2 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64",
        "ansible_distribution": "CentOS",
        "ansible_distribution_release": "7",
        ...
    },
    "changed": false
}

Use case 4: Execute a command on a group of hosts

Code:

ansible group -m command -a 'my_command'

Motivation: Executing a command on multiple hosts simultaneously saves time and allows you to perform tasks efficiently, without the need to log in to each host separately.

Explanation:

  • ansible: The main command.
  • group: The name of the group on which you want to execute the command.
  • -m command: Specifies the module to invoke. In this case, the command module is used to execute the command.
  • -a 'my_command': The argument that defines the command to be executed. Replace ‘my_command’ with the actual command you want to run.

Example output:

host1 | SUCCESS | rc=0 >>
Output of my_command on host1

host2 | SUCCESS | rc=0 >>
Output of my_command on host2

Use case 5: Execute a command with administrative privileges

Code:

ansible group --become --ask-become-pass -m command -a 'my_command'

Motivation: Some commands require administrative privileges to be executed. By using the --become option, you can run commands with elevated privileges, allowing you to perform administrative tasks without manually switching user accounts.

Explanation:

  • ansible: The main command.
  • group: The name of the group on which you want to execute the command.
  • --become: An option that tells Ansible to become another user, usually with administrative privileges.
  • --ask-become-pass: An option that prompts you to enter the password for the privileged user.
  • -m command: Specifies the module to invoke. In this case, the command module is used to execute the command.
  • -a 'my_command': The argument that defines the command to be executed. Replace ‘my_command’ with the actual command you want to run.

Example output:

BECOME password:
host1 | SUCCESS | rc=0 >>
Output of my_command on host1

host2 | SUCCESS | rc=0 >>
Output of my_command on host2

Use case 6: Execute a command using a custom inventory file

Code:

ansible group -i inventory_file -m command -a 'my_command'

Motivation: Ansible uses the /etc/ansible/hosts file as its default inventory. However, in some scenarios, you may want to use a custom inventory file to manage your hosts. This command allows you to specify a custom inventory file when executing commands.

Explanation:

  • ansible: The main command.
  • group: The name of the group on which you want to execute the command.
  • -i inventory_file: An option that specifies the custom inventory file to use.
  • -m command: Specifies the module to invoke. In this case, the command module is used to execute the command.
  • -a 'my_command': The argument that defines the command to be executed. Replace ‘my_command’ with the actual command you want to run.

Example output:

host1 | SUCCESS | rc=0 >>
Output of my_command on host1

host2 | SUCCESS | rc=0 >>
Output of my_command on host2

Use case 7: List the groups in an inventory

Code:

ansible localhost -m debug -a 'var=groups.keys()'

Motivation: Having an overview of the groups defined in the inventory file allows you to better understand the organization of your hosts and identify any issues or inconsistencies in the configuration.

Explanation:

  • ansible: The main command.
  • localhost: The target host on which to execute the command. Using “localhost” ensures that the command is executed on the machine running Ansible.
  • -m debug: Specifies the module to invoke. In this case, the debug module is used to display the groups.
  • -a 'var=groups.keys()': The argument that instructs Ansible to display the keys (i.e., group names) of the “groups” variable.

Example output:

localhost | SUCCESS => {
    "groups.keys()": [
        "group1",
        "group2",
        "group3",
        ...
    ]
}

Conclusion:

The “ansible” command is a versatile tool for managing groups of computers remotely. With the provided examples, you can perform a wide range of tasks such as listing hosts, pinging hosts, gathering facts, executing commands, and more. By leveraging the power of Ansible, you can automate various aspects of system administration and configuration management, making your infrastructure more efficient and scalable.

Related Posts

How to use the command shasum (with examples)

How to use the command shasum (with examples)

The shasum command is used to calculate SHA cryptographic checksums. It is typically used to verify the integrity of files by comparing their checksums before and after transmission or storage.

Read More
Using opensnoop (with examples)

Using opensnoop (with examples)

Tracking all file opens To track all file opens as they occur on your system, you can simply use the sudo opensnoop command.

Read More
How to use the command xfce4-screenshooter (with examples)

How to use the command xfce4-screenshooter (with examples)

The xfce4-screenshooter command is a screenshot tool specifically designed for the XFCE desktop environment.

Read More