Installing Ansible on Ubuntu Bionic Beaver
- ansible
- getting-started
- orchestration
Ansible Overview
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 systems 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.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have created an Instance which is running Ubuntu Bionic
- You have configured your SSH key
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:If you use the root user, you can remove the
sudo
before each command. -
Update Ubuntu package manager:
sudo apt-get update -
Upgrade the Ubuntu packages already installed:
sudo apt-get 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 2.6.1config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/dist-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
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 to 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 in order for Ansible to communicate with it.
Note:Python 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 in the inventory.
- hosts: allvars:ansible_python_interpreter: /usr/bin/env python3host1 ansible_ssh_host=X.X.X.X ansible_python_interpreter=/usr/bin/env python3Important:Under 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