NavigationContentFooter
Jump toSuggest an edit

Installing Ansible on Ubuntu Bionic Beaver

Reviewed on 16 September 2024Published on 26 July 2018
  • 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

  1. Connect to your server using SSH:

    ssh root@SERVER_IP

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

  2. Update the Ubuntu package manager:

    apt update
  3. Upgrade the Ubuntu packages already installed:

    apt upgrade

Installing Ansible from PPA repository

  1. 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-common
    apt-add-repository ppa:ansible/ansible
    apt update
  2. Install the Ansible software

    apt install ansible
  3. Check that the installation is successful

    ansible --version

    which returns

    ansible [core 2.16.4]
    config file = /etc/ansible/ansible.cfg
    configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python3/dist-packages/ansible
    ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
    executable location = /usr/bin/ansible
    python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
    jinja version = 3.0.3
    libyaml = 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

  1. Generate an SSH key

    ssh-keygen -t rsa

    which 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:
  2. Optionally, to avoid the prompt of your passphrase, launch exec ssh-agent $SHELL to run an SSH agent, and ssh-add ~/.ssh/id_rsa to add your key to the SSH agent.

  3. 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
  4. 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
  5. Open the authorized_keys within the ~/.ssh directory

    nano ~/.ssh/authorized_keys
  6. In the file, paste your Ansible server user’s SSH key, then save the file and close the editor.

  7. Install Python 3 on the host 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 update
    apt install python3
  8. To make Ansbile work with Python 3, specify the Python interpreter in a var or the inventory.

    - hosts: all
    vars:
    ansible_python_interpreter: /usr/bin/env python3
    host1 ansible_ssh_host=X.X.X.X ansible_python_interpreter=/usr/bin/env python3
    Important

    Under Credentials, paste your SSH key in the Scaleway console and click Use this SSH key.

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

  1. 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/hosts

    In 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_ip

    In this example, group_name is an organizational tag that lets you refer to any servers listed under it with one word, while alias 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.X
    host2 ansible_ssh_host=X.X.X.X
  2. 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
API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway