In this section, we will cover the fundamental Ansible commands that you need to know to start managing your infrastructure. These commands will help you perform various tasks such as checking connectivity, gathering information, and executing tasks on remote hosts.

Key Concepts

Before diving into the commands, let's understand some key concepts:

  • Control Node: The machine where Ansible is installed and from which commands are run.
  • Managed Nodes: The machines that are managed by Ansible.
  • Inventory: A list of managed nodes.
  • Modules: Units of work that Ansible executes on managed nodes.

Common Ansible Commands

  1. ansible --version

This command checks the installed version of Ansible.

ansible --version

Output:

ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Feb 26 2020, 02:56:10) [GCC 9.2.1 20191008]

  1. ansible all -m ping

This command checks the connectivity to all hosts in the inventory using the ping module.

ansible all -m ping

Output:

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

  1. ansible all -m setup

This command gathers facts about the managed nodes. The setup module collects detailed information about the system.

ansible all -m setup

Output:

host1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.1.10"
        ],
        "ansible_architecture": "x86_64",
        ...
    },
    "changed": false
}

  1. ansible all -a "uptime"

This command runs an ad-hoc command to check the uptime of all hosts in the inventory.

ansible all -a "uptime"

Output:

host1 | CHANGED | rc=0 >>
 10:23:45 up 5 days,  3:45,  1 user,  load average: 0.00, 0.01, 0.05
host2 | CHANGED | rc=0 >>
 10:23:45 up 10 days,  2:30,  2 users,  load average: 0.01, 0.02, 0.00

  1. ansible all -m command -a "df -h"

This command uses the command module to check disk space on all hosts.

ansible all -m command -a "df -h"

Output:

host1 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   15G  4.5G  77% /
host2 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   30G   20G  60% /

  1. ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"

This command uses the copy module to copy the /etc/hosts file to /tmp/hosts on all hosts.

ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"

Output:

host1 | SUCCESS => {
    "changed": true,
    "checksum": "3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c",
    "dest": "/tmp/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c3b3c",
    "mode": "0644",
    "owner": "root",
    "size": 258,
    "src": "/root/.ansible/tmp/ansible-tmp-1587654321.123456-1234567890.123456/hosts",
    "state": "file",
    "uid": 0
}

Practical Exercises

Exercise 1: Check Connectivity

  1. Ensure you have an inventory file named hosts with at least one host.
  2. Run the ansible all -m ping command to check connectivity.

Solution:

ansible all -m ping

Exercise 2: Gather System Information

  1. Use the setup module to gather information about your managed nodes.
  2. Save the output to a file named system_info.txt.

Solution:

ansible all -m setup > system_info.txt

Exercise 3: Check Disk Space

  1. Run an ad-hoc command to check the disk space on all managed nodes.
  2. Use the df -h command.

Solution:

ansible all -a "df -h"

Common Mistakes and Tips

  • Inventory File: Ensure your inventory file is correctly formatted and accessible.
  • SSH Access: Make sure you have SSH access to the managed nodes.
  • Module Arguments: Always check the required arguments for each module to avoid errors.

Conclusion

In this section, we covered the basic Ansible commands that are essential for managing your infrastructure. You learned how to check connectivity, gather system information, and execute tasks on remote hosts. These commands form the foundation for more advanced Ansible operations, which we will explore in the upcoming modules.

© Copyright 2024. All rights reserved