DEV Community

Cover image for Ansible 101: Inventory Files & Ad-hoc Cmds
Javel Rowe
Javel Rowe

Posted on • Updated on • Originally published at javel.dev

Ansible 101: Inventory Files & Ad-hoc Cmds

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
Enter fullscreen mode Exit fullscreen mode

inventory.yaml

webservers:
  hosts:
    170.187.144.11
    170.187.144.12
    170.187.144.13

Enter fullscreen mode Exit fullscreen mode

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.


Enter fullscreen mode Exit fullscreen mode

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.


Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. Spin up the environment using docker-compose

docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

3. SSH into the Ansible server

ssh -o StrictHostKeyChecking=no -o NoHostAuthenticationForLocalhost=yes root@localhost -p 2200# password: test123
Enter fullscreen mode Exit fullscreen mode

4. Change directory to ansible_learn

cd ansible_learn
Enter fullscreen mode Exit fullscreen mode

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


touch inventory.yaml

nano inventory.yaml

# Paste the following in your inventory.yaml file.
# webservers:
#  hosts:
#    server1:
#    server2:
#    server3:
Enter fullscreen mode Exit fullscreen mode

6. 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

Enter fullscreen mode Exit fullscreen mode

7. 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

Enter fullscreen mode Exit fullscreen mode

8. 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

Enter fullscreen mode Exit fullscreen mode

9. Stop and remove containers to clean up

docker compose down

Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)