In this lab, you will create a group and add a machine to that group. This involves creating an Inventory
file for Ansible.
Estimated Time: 25 minutes
Ensure that ansible
is installed on your system.
Navigate to the Ansible directory:
cd /etc/ansible
# To list the files in the directory
ls
Output:
ansible.cfg hosts
ansible.cfg
: This is the Ansible configuration file.hosts
: This file is used to store the inventory of your hosts and their configurations.
Notes:
- The
hosts
file is also known as the inventory file. - There could be another files and directories in the
/etc/ansible
directory, but for this lab, we are only interested in thehosts
file.
Add a new group to the /etc/ansible/hosts
file. To do this, open the file for editing:
sudo nano /etc/ansible/hosts
At the end of the file, add the following group:
[webservers]
- Group names are case-sensitive and must be enclosed in square brackets
[]
. - Group names must be unique.
- Avoid spaces or special characters in group names.
- Use meaningful names that are easy to remember for future labs.
Keep the file open and proceed to the next step.
Add the designated server(s) to your group in the hosts
file. For example:
[webservers]
<IP Machine 1> ansible_ssh_pass=<Machine 1 Password>
Keep the file open and continue to the next step.
Since Ansible uses SSH to connect to hosts, you need to specify the username and password. This is done using the :vars
syntax.
For the [webservers]
group, add [webservers:vars]
after the last host entry:
ansible_user
: The username to use for SSH.ansible_ssh_pass
: The password to use for SSH.
[webservers]
<IP Machine 1> ansible_ssh_pass=<Machine 1 Password>
[webservers:vars]
ansible_user=<Username> # Example: ubuntu in this lab environment
Save the file by pressing Ctrl+S
, and exit by pressing Ctrl+X
.
You have successfully created a group, added a host, and defined its connection details in the Ansible inventory! We will use this inventory in the upcoming labs to run playbooks on the hosts you have defined.