Setting Up the Environment
Before we dive into Ansible conditionals and loops, let's set up our working environment. This step is crucial as it prepares the groundwork for all our subsequent tasks.
First, let's navigate to the project directory. In the terminal, type:
cd ~/project
This command changes your current directory to ~/project
, which is our designated workspace for this lab.
Now, we'll create an inventory file. In Ansible, an inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. For this lab, we'll use a simple inventory that includes only the localhost.
Create a new file named inventory.ini
:
nano inventory.ini
This command opens the nano text editor. If you're not familiar with nano, don't worry - it's a simple, user-friendly text editor. The cursor will be placed in the file, ready for you to type.
Now, let's add the following content to the file:
[local]
localhost ansible_connection=local
Let's break this down:
[local]
defines a group named "local"
localhost
is the name of the host
ansible_connection=local
tells Ansible to run commands locally instead of over SSH
To save the file and exit nano:
- Press
Ctrl + X
- You'll be asked if you want to save the modified buffer. Press
Y
for yes.
- Press
Enter
to confirm the file name.
Next, let's create a directory for our playbooks:
mkdir playbooks
cd playbooks
The mkdir
command creates a new directory named "playbooks", and then we use cd
to move into this new directory.
Why are we doing this? Organizing your Ansible files into directories is a best practice. It keeps your project organized, especially as it grows larger and more complex.
By following these steps, you've now set up a basic Ansible environment. You have an inventory file that tells Ansible what hosts to work with, and a dedicated directory for your playbooks. This structure will make it easier to manage your Ansible projects as you learn and experiment with more complex features.