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 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 file. After which you can use Ansible CLI to run to execute tasks on the servers you listed. Inventory ad-hoc commands 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 or format. Here's an example that defines a group of webservers: .ini .yaml 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 is commonly used in many config files and allows you to give each host a unique name so I'd recommend using that. .yaml 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 and allows you to execute simple one-time commands. Using the file we created above, we can use to do the following: ad-hoc command inventory.ini ad-hoc commands 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 Exercise I've created a file that will provision an environment that will allow you to practice what we learned above. docker-compose Setup Clone the repo git clone https://github.com/perplexedyawdie/ansible-learn.git2. Change directory to inventory-files-and-ad-hoc-cmds cd ansible-learn/inventory-files-and-ad-hoc-cmds 3. Spin up the environment using docker-compose docker compose up -d --build 4. SSH into the Ansible server ssh -o StrictHostKeyChecking=no root@localhost -p 2200 # password: test123 5. Create an file and populate with hosts inventory.yaml cd ansible_learn touch inventory.yaml nano inventory.yaml # Paste the following in your inventory.yaml file. # webservers: # hosts: # server1: # server2: # server3: 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 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 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 9. 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 , we'll look at Playbooks along with task control mechanisms and handlers. next article Also published here.