file

Ansible is a configuration management system written in the Python programming language. It is used to automate software configuration and deployment.It is an open source tool for managing software, configuration and deployment of applications across multiple hosts

Installation
First, update the package index:
``.shell
sudo apt update

Use the PPA repository for installation. Install software-properties-common, a package that makes it easy to manage independent software repositories.  
``.shell
sudo apt install software-properties-common  

Then add the Ansible PPA repository using the following command:
``.shell
sudo apt-add-repository ppa:ansible/ansible

Press the Enter key to join.  

Once again, update the package base and install Ansible:  
``.shell
sudo apt update  
sudo apt install ansible  

After that, the Ansible software needed to administer your hosts will be installed on the server.

Setting up access to hosts via SSH

Use the following command to create an SSH key that will be used to connect to the hosts:
``.shell
ssh-keygen -t rsa -b 4096

On the Ansible server, use the cat command to display the SSH public keys in the terminal:  
``.shell
cat ~/.ssh/id_rsa.pub  

``.shell
root@kvmde67:~# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa)
Enter passphrase (empty for no passphrase)
Enter same passphrase again
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Zvr1I9IfDK3wCK9K5h5KtWBtC0R2ppUtgQOB/fikhNE root@kvmde67.fornex.org
The key's randomart image is:
+---[RSA 4096]----+
|+=o.=+ |
|ooE=o . |
| oo+ . |
|..o.o . |
| .+++ . S . . |
| ..=.o * + + |
| . * . o.+ o |
| . = . o ... o ... |
| ..+.. .. oo. |
+----[SHA256]-----+
root@kvmde67:~# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQClTC6YhDZdHhiTj2UjDdOv5gpJufT0YKF6tnE1YwIFRrROBH8+hG7hyNlRtc7c5NGTp7fXXoVhnFRFe+thN/4ZDJ/1A44Pe91NldOBVovMlulk9RsRQqvLX8sZXLzY17i+2DINVF3aoOh6o3QEHrk3Axh+qz5DUYvJ70wM6GM40szUBf0wpA1GwoUaOmT4oZVDN6xRT05k++DGcik/EgwWy8hWK8hW8fmg5kXiWSvslLM8i/URaFFtTiFrna1NB75Rbl9brMLc3xMwUzqXRnohRBGbP/M7Js7HoaiS3yHqxIeo2JPVfdmxwpdCB2y14FTG0GU7gTeEOg6BUYNsSlU6IKv593RIGZ9hyPGnDiTZd3jgaOdOoYTKiHWj23zlrqqwSWbNQ== root@kvmde67.fornex.org

Copy the generated key to each host to be served by Ansible:  
``.shell
ssh-copy-id user_name@ip-address  

For example: ssh-copy-id root@5.187.2.32

Use a text editor to open the following configuration file:
``.shell
sudo nano /etc/ansible/hosts

Using the following syntax, the [servers] group is defined with two different servers, each with its own indentifier: server1, server2. Be sure to replace the allocated IP addresses with the IP addresses of your Ansible hosts.  

``.shell
[group_name] 
server1 ansible_host=your_server_ip_1  
server2 ansible_host=your_server_ip_2  

[servers:vars]
ansible_python_interpreter=/usr/bin/python3  

The servers:vars subgroup sets the ansible_python_interpreter host parameter, which will be valid for all hosts in the servers group. With this parameter value the remote server uses the Python 3 /usr/bin/python3 executable, not /usr/bin/python (Python 2.7)

For example:
``.shell
[servers]
server1 ansible_host=5.187.2.32
server2 ansible_host=5.187.5.236

[servers:vars]
ansible_python_interpreter=/usr/bin/python3


Save your changes and close the file (CTRL + X, Y, Enter)  

Important: The python language and the python-apt package must be installed on each host:  
``.shell
apt-get install python python-apt  

Check connectivity

By default Ansible connects to the remote host as root, if you use a different user you need to make additional settings. Create a directory in the configuration structure that contains the YAML files for each group:
``.shell
sudo mkdir /etc/ansible/group_vars

Create a subdirectory with the name of your group:  
``.shell
sudo nano /etc/ansible/group_vars/group_name  

For example:

sudo nano /etc/ansible/group_vars/servers  

Insert the following line with your user name instead of user_name:
``.shell


ansible_user: user_name

Save your changes.  

Note: individual hosts can be configured by creating aliases and naming files according to their alias in /etc/ansible/host_vars  

To test the connection, run the ping command:  
``.shell
ansible -m ping all  

Result:
``.shell
server1 | SUCCESS => {
{ "changed": false,
{ "ping": "pong"
}
server2 | SUCCESS => {
{ "changed": false,
"ping": "pong"
}

```
file

Updated April 20, 2020