If you use the root user, you can remove the sudo
before each command.
Installing Ansible on Ubuntu Bionic Beaver
- ansible
- orchestration
Ansible is an IT automation tool. It simplifies cloud computing, configuration management, program setup, intra-service orchestration, and several other IT needs.
Ansible uses a very simple language (YAML, in the form of Ansible Playbooks) that allows you to spell out your automation jobs in a way that means plain English.
While there are many popular configuration management systems available for Linux systems, such as Chef, Ansible is the simplest configuration management system to get started with.
Ansible works by configuring client machines from a computer that has the Ansible components installed and configured. It communicates over standard SSH channels to retrieve information from remote machines. This means that any computer that you can administer through SSH, you can also administer through Ansible.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- An Instance running Ubuntu Bionic
Installing Ansible 2.6 on Ubuntu Bionic Beaver
-
Connect to your server using SSH:
ssh root@SERVER_IPIf you do not know your server IP, you can list your existing servers using
scw ps
(Scaleway CLI). For more information on the Scaleway CLI, refer to the tutorial on the Scaleway Command Line Interface.The server IP can also be retrieved from the Scaleway console. Once logged in, check the IP Adresses in the Servers tab of the left menu.
Note -
Update the Ubuntu package manager:
apt update -
Upgrade the Ubuntu packages already installed:
apt upgrade
Installing Ansible from PPA repository
-
Update your package index and install the
software-properties-common package
. This software will make it easier to manage this and other independent software repositories. Add the Ansible PPA and refresh your system’s package index once again.apt install software-properties-commonapt-add-repository ppa:ansible/ansibleapt update -
Install the Ansible software
apt install ansible -
Check that the installation is successful
ansible --versionwhich returns
ansible [core 2.16.4]config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3/dist-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)jinja version = 3.0.3libyaml = True
Alternative Installation of Ansible
To learn more about different methods for installing Ansible, refer to the official Ansible Documentation.
Configuring SSH Access to the Ansible Hosts
-
Generate an SSH key
ssh-keygen -t rsawhich returns
Enter file in which to save the key (/home/user/.ssh/id_rsa):It is recommended to press
Enter
to generate and store the SSH key in the default location.Enter passphrase (empty for no passphrase):Enter same passphrase again: -
Optionally, to avoid the prompt of your passphrase, launch
exec ssh-agent $SHELL
to run an SSH agent, andssh-add ~/.ssh/id_rsa
to add your key to the SSH agent. -
Use the cat command to print the contents of your non-root user’s SSH public key file to the terminal’s output
cat ~/.ssh/id_rsa.pub -
Copy the resulting output to your clipboard, then open a new terminal and connect to one of your Ansible hosts using SSH
ssh root@ansible_host_ip -
Open the
authorized_keys
within the~/.ssh
directorynano ~/.ssh/authorized_keys -
In the file, paste your Ansible server user’s SSH key, then save the file and close the editor.
-
Install Python 3 on the host for Ansible to communicate with it.
NotePython 2 is almost at its EOF and Ubuntu Bionic Beaver does not integrate version 3 by default.
apt updateapt install python3 -
To make Ansbile work with Python 3, specify the Python interpreter in a var or the inventory.
- hosts: allvars:ansible_python_interpreter: /usr/bin/env python3host1 ansible_ssh_host=X.X.X.X ansible_python_interpreter=/usr/bin/env python3ImportantUnder Credentials, paste your SSH key in the Scaleway console and click Use this SSH key.
-
Run the exit command to close the connection to the client Repeat this process for each server you intend to control with your Ansible server.
Next, we’ll configure the Ansible server to connect to these hosts using Ansible’s hosts file.
Configuring Ansible Hosts
-
Ansible tracks of all the servers through an inventory file. We need to set up this file first before we can communicate with our other computers.
On your Ansible server, open the file
sudo nano /etc/ansible/hostsIn our example, we have two servers controlled with Ansible. The hosts file is fairly flexible and can be configured in a few different ways. The syntax we are going to use, though, looks like this:
[group_name]alias ansible_ssh_host=your_server_ipIn this example,
group_name
is an organizational tag that lets you refer to any servers listed under it with one word, whilealias
is just a name to refer to one specific server. For the tutorial purpose, our host file looks like this:[servers]host1 ansible_ssh_host=X.X.X.Xhost2 ansible_ssh_host=X.X.X.X -
Save and close this file when you are finished.
If you want to specify configuration details for every server, regardless of group association, you can put those details in a file at
/etc/ansible/group_vars/all
. Individual hosts can be configured by creating files named after their alias under a directory at/etc/ansible/host_vars
.
Using Ansible Commands
Ping all servers
ansible -m ping all
which returns
host1 | SUCCESS => {"changed": false,"ping": "pong"}host2 | SUCCESS => {"changed": false,"ping": "pong"}
The all
means all hosts listed in the hosts file. However, it is also possible to:
- specify a group:
ansible -m ping servers
- specify an individual host:
ansible -m ping host1
- specify multiple hosts by separating them with colons:
ansible -m ping host1:host2
For more information on Ansible commands or playbook, refer to the official Ansible documentation.
Going further
- USE CASE 1: Configuring Apache Using Ansible
- USE CASE 2: Configuring Ansible Galaxy