4 minutes
Ansible - Getting Start With Ansible - Part 1
Introduction to Ansible
Ansible is an open-source automation engine that automates software provisioning, configuration management, and application deployment. Unlike many other automation tools, Ansible is agentless, meaning it doesn’t require any special software or daemons to be installed on the managed nodes. It communicates with machines over standard SSH for Linux/Unix and WinRM for Windows, making it incredibly easy to get started.
Key Concepts:
- Inventory: A list of managed hosts (servers, network devices, etc.) that
Ansiblecan manage. - Playbooks:
YAMLfiles that define a set of tasks to be executed on managed hosts. They are the core ofAnsible’s configuration, deployment, and orchestration language. - Tasks: Individual actions performed by
Ansible, such as installing a package, copying a file, or starting a service. - Modules: Reusable units of code that
Ansibleexecutes to perform specific tasks.Ansibleships with a vast library of modules. - Roles: A way to organize playbooks, variables, templates, and other files into a reusable and shareable structure.
- Facts: Information gathered by
Ansibleabout the managed nodes (e.g.,operating system,IP address,memory).
Why Choose Ansible?
- Agentless: No agents to install or manage on your target systems.
- Simple and Human-Readable: Uses
YAMLforplaybooks, which is easy to read and write. - Powerful and Flexible: Can automate a wide range of IT tasks, from server provisioning to application deployment.
- Extensible: Easy to write custom modules and plugins.
- Active Community: Large and supportive community with extensive documentation.
Installation
Ansible can be installed on various Linux distributions, macOS, and even Windows (via WSL). The recommended way to install Ansible is using pip, the Python package installer.
Prerequisites
- Python 3 (3.8 or newer recommended)
- pip (Python package installer)
Installation Steps
- Install Python and pip (if not already present):
On Debian/Ubuntu:
sudo apt update
sudo apt install python3 python3-pip
On CentOS/RHEL/Fedora:
sudo dnf install python3 python3-pip
On macOS:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python
- Install Ansible using pip:
pip3 install ansible
- Verify Installation:
ansible --version
You should see output similar to this, indicating the installed Ansible version and other details:
ansible [core 2.15.5]
config file = null
configured module search path = ['/home/youruser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/youruser/.local/lib/python3.x/site-packages/ansible
ansible collection location = /home/youruser/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.x.x (main, ...) [GCC ...]
jinja version = 3.x.x
libyaml = True
Basic Configuration: The Inventory File
The inventory file tells Ansible which hosts it can manage. By default, Ansible looks for an inventory file at /etc/ansible/hosts. However, for most use cases, especially development and testing, you’ll create a local inventory file.
Creating an Inventory File
Create a file named inventory.ini (or any name you prefer) in your project directory.
# inventory.ini
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
[all:vars]
ansible_user=your_ssh_user
ansible_private_key_file=~/.ssh/id_rsa
# ansible_ssh_pass='your_password' # Use this only if you don't have SSH keys setup or for testing
Explanation:
- [webservers], [databases]: These are groups. You can group your hosts logically.
- web1.example.com, db1.example.com: These are the actual
hostnamesorIP addressesof your target machines. - [all:vars]: This section defines variables that apply to all hosts in the inventory.
- ansible_user: The SSH user to connect to the remote machines.
- ansible_private_key_file: The path to your
SSH private key. This is the recommended way to connect. - ansible_ssh_pass: (Use with caution!) This variable allows you to specify the
SSHpassword directly. It’s generally not recommended for production environments due to security concerns. UseSSHkeys instead.
Testing Connectivity
Once your inventory is set up, you can test connectivity to your hosts using the ansible command (not ansible-playbook yet).
- Ping all hosts:
ansible all -i inventory.ini -m ping
- all: Targets all hosts defined in your inventory.
- -i inventory.ini: Specifies your inventory file. If you use the default
/etc/ansible/hosts, you can omit this. - -m ping: Uses the built-in ping module to test connectivity.
Successful output will look like this:
web1.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
db1.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
- Ping a specific group:
ansible webservers -i inventory.ini -m ping
- Run an ad-hoc command:
You can run single commands on your remote hosts without creating a playbook.
ansible webservers -i inventory.ini -a "uptime"
- -a “uptime”: Executes the uptime command on the target hosts.
Output:
web1.example.com | SUCCESS | rc=0 >>
10:30:00 up 1 day, 2:34, 1 user, load average: 0.00, 0.01, 0.05
Next Steps
In the next part, we will dive into writing your first Ansible Playbook, understanding tasks, modules, and basic playbook structure.