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
ansible --version
ansible --version
This command checks the installed version of Ansible.
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]
ansible all -m ping
ansible all -m ping
This command checks the connectivity to all hosts in the inventory using the ping
module.
Output:
host1 | SUCCESS => { "changed": false, "ping": "pong" } host2 | SUCCESS => { "changed": false, "ping": "pong" }
ansible all -m setup
ansible all -m setup
This command gathers facts about the managed nodes. The setup
module collects detailed information about the system.
Output:
host1 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.1.10" ], "ansible_architecture": "x86_64", ... }, "changed": false }
ansible all -a "uptime"
ansible all -a "uptime"
This command runs an ad-hoc command to check the uptime of all hosts in the inventory.
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
ansible all -m command -a "df -h"
ansible all -m command -a "df -h"
This command uses the command
module to check disk space on all hosts.
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% /
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
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.
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
- Ensure you have an inventory file named
hosts
with at least one host. - Run the
ansible all -m ping
command to check connectivity.
Solution:
Exercise 2: Gather System Information
- Use the
setup
module to gather information about your managed nodes. - Save the output to a file named
system_info.txt
.
Solution:
Exercise 3: Check Disk Space
- Run an ad-hoc command to check the disk space on all managed nodes.
- Use the
df -h
command.
Solution:
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.
Ansible: From Beginner to Advanced
Module 1: Introduction to Ansible
Module 2: Ansible Basics
Module 3: Playbooks
- Introduction to Playbooks
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
Module 4: Roles
Module 5: Advanced Playbook Techniques
Module 6: Ansible Galaxy
Module 7: Ansible Tower
- Introduction to Ansible Tower
- Installing Ansible Tower
- Using Ansible Tower
- Managing Projects and Inventories