Setting Up the Ansible Environment
Installing Ansible
To get started with Ansible, you'll first need to install it on your control node (the machine from which you'll be running Ansible commands). In this example, we'll be using Ubuntu 22.04 as the control node.
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Once the installation is complete, you can verify the installation by running the following command:
ansible --version
This should display the version of Ansible installed on your system.
Configuring Ansible
Ansible's configuration is stored in the /etc/ansible/ansible.cfg
file. You can customize this file to suit your needs, such as setting the default inventory file, the remote user, or the SSH connection parameters.
Here's an example of a basic ansible.cfg
file:
[defaults]
inventory = ./hosts
remote_user = ubuntu
private_key_file = ~/.ssh/id_rsa
In this example, we've set the inventory file to ./hosts
, the remote user to ubuntu
, and the private key file to ~/.ssh/id_rsa
.
Creating an Inventory
The inventory file is where you define the hosts that Ansible will manage. You can use various formats, such as a simple text file or a dynamic inventory script.
Here's an example of a simple inventory file (hosts
):
[webservers]
web01 ansible_host=192.168.1.100
web02 ansible_host=192.168.1.101
[databases]
db01 ansible_host=192.168.1.150
db02 ansible_host=192.168.1.151
In this example, we've defined two groups: webservers
and databases
, each with two hosts.
Now that you've set up your Ansible environment and created an inventory, you're ready to run your first Ansible playbook.