Skip to content
Cloud Kung-Fu

Inventory Files & Ad-hoc Cmds

3 min read

What is Ansible?

This is a task automation tool that uses scripts to run commands on remote or local systems. A core idea is that you input the state you want the system to be in (e.g: have docker installed) then Ansible will ensure that it maintains this state.

Basic Architecture

Ansible Architecture

The main idea is to have the Ansible CLI installed on a machine you have access to, then you'll create a list of servers and IP addresses in what's called an Inventory file. After which you can use Ansible CLI to run ad-hoc commands to execute tasks on the servers you listed.

Inventory Files

This is where you define the list of hosts (this can be a local instance of a machine, remote servers etc) that you want to manage using Ansible. These files can be in .ini or .yaml format. Here's an example that defines a group of webservers:

inventory.ini

[webservers]
170.187.144.11
170.187.144.12
170.187.144.13

inventory.yaml

webservers:
hosts:
170.187.144.11
170.187.144.12
170.187.144.13

Both formats accomplish the same thing, however .yaml is commonly used in many config files and allows you to give each host a unique name so I'd recommend using that.

Ad-hoc Commands

To automate a task, you can use scripts that contain specific commands or you can run commands directly in the CLI. The latter is referred to as an ad-hoc command and allows you to execute simple one-time commands. Using the inventory.ini file we created above, we can use ad-hoc commands to do the following:

Ping

ansible webservers -m ping --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml
# -m Tells Ansible which module to use, in this case, ping
# --key-file Specifies the SSH private key file to use for connections to the remote hosts.
# -u Indicates that Ansible should connect as the `root` user.
# -i Specifies the inventory file to use.

Copy

ansible webservers -m copy -a "src=test.txt dest=/root/" --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml
# -a Specifies the arguments to pass to the module. This module copies files from the local machine on which Ansible is running to the remote hosts specified in the inventory.yaml file.

Practice

I've created a docker compose file that will provision an environment that will allow you to practice what we learned above.

Setup

  1. Clone the repo
git clone https://github.com/perplexedyawdie/ansible-learn.git

2. Spin up the environment using docker-compose

docker compose up -d --build

3. SSH into the Ansible server

ssh -o StrictHostKeyChecking=no root@localhost -p 2200
# password: test123

4. Create an inventory.yaml file and populate with hosts

cd ansible_learn
touch inventory.yaml
nano inventory.yaml
# Paste the following in your inventory.yaml file.
# webservers:
# hosts:
# server1:
# server2:
# server3:

5. Ping the hosts to ensure that you can connect to them

ansible webservers -m ping --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml

6. Copy a file to each host

echo "Hello Mate!!" >> test.txt
ansible webservers -m copy -a "src=test.txt dest=/root/" --key-file /root/.ssh/id_rsa_ansible -u root -i inventory.yaml

7. SSH into a host to see your copied file

ssh -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa_ansible root@server1
ls
cat test.txt
exit
exit

8. Stop and remove containers to clean up

docker compose down

Recap

You just learned the basics of Ansible automation! Specifically, how to define the hosts you want to remotely manage and how to execute commands on them. In the next article, we'll look on Playbooks along with task control mechanisms and handlers.

© 2024 by Cloud Kung-Fu. All rights reserved.
Theme by LekoArts