Create Inventory Directory and Files
Let's start by creating a directory to store our inventory files and then create two separate inventory files for development and production environments.
First, create a new directory called inventory
in the /home/labex/project
path:
mkdir -p /home/labex/project/inventory
This command creates a new directory called inventory
. The -p
option ensures that the parent directories are created if they don't exist.
Now, create two inventory files: dev_inventory.ini
and prod_inventory.ini
inside the inventory
directory:
touch /home/labex/project/inventory/dev_inventory.ini
touch /home/labex/project/inventory/prod_inventory.ini
The touch
command creates empty files if they don't exist, or updates the modification time if they do exist.
Next, we'll add content to these inventory files. Open dev_inventory.ini
in a text editor (you can use nano
or any other text editor you're comfortable with):
nano /home/labex/project/inventory/dev_inventory.ini
Add the following content:
[dev]
localhost ansible_connection=local
Save the file and exit the editor (in nano, you can do this by pressing Ctrl+X, then Y, then Enter).
Now, open prod_inventory.ini
:
nano /home/labex/project/inventory/prod_inventory.ini
Add the following content:
[prod]
localhost ansible_connection=local
Save and exit the editor.
In these inventory files:
[dev]
and [prod]
are group names. They help organize hosts into logical groups.
localhost
is the hostname. We're using localhost to simulate different environments on the same machine.
ansible_connection=local
tells Ansible to run commands locally instead of trying to SSH into the machine. This is useful for running Ansible against the same machine it's installed on.
In a real-world scenario, you would typically have different IP addresses or hostnames for your development and production servers instead of using localhost for both.