This post will guide you how to install and configure the Ansible on an Ubuntu 18.04 Linux server. How do I install the Ansible on Ubuntu 16.04/18.04 from a standard Ubuntu repository, or PPA repository. How to install the latest version of the Ansible by compiling the source code on your Ubuntu 18.04 Linux.
- What is Ansible
- Install Ansible from Ubuntu Default Repository
- Install Ansible from Ubuntu PPA Repository
- Install Ansible from a Source Package
- Configure Ansible Host
- Setup SSH Keys for Remove Ansible Host
- Testing Ansible
What is Ansible
Ansible is an open-source software provisioning, configuration management, and application deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible is a simple automation language that can perfectly describe an IT application infrastructure. It’s easy-to-learn, self-documenting, and doesn’t require a grad-level computer science degree to read. Automation shouldn’t be more complex than the tasks it’s replacing.
Install Ansible from Ubuntu Default Repository
You can use the default Ubuntu repository to install the Ansible package by using the apt or apt-get command in your terminal of Ubuntu 18.04 Linux server. So you can open up a terminal and then type the following command:
$ sudo apt install ansible
Outputs:
devops@devops-osetc:~$ sudo apt install ansible
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
amd64-microcode btrfs-tools docutils-common gcc-6-base:i386 intel-microcode iucode-tool libboost-random1.58.0 libboost-thread1.58.0
libcapnp-0.5.3 libevent-core-2.0-5 libhardware2 libhybris libhybris-common1 libiscsi2 libmedia1 librpm3 librpmbuild3 librpmio3
librpmsign3 libwebpdemux1 libwxgtk-media3.0-0v5 libwxgtk-media3.0-dev libwxgtk3.0-0v5 libwxgtk3.0-dev libxen-4.6 linux-generic
linux-generic-hwe-16.04 linux-headers-generic linux-image-generic python3-botocore python3-colorama python3-dateutil
python3-docutils python3-jmespath python3-pygments python3-roman python3-rsa python3-s3transfer qml-module-qtwebkit snap-confine
snapd-login-service thermald xserver-xorg-input-evdev-hwe-16.04 xserver-xorg-input-synaptics-hwe-16.04
xserver-xorg-video-amdgpu-hwe-16.04 xserver-xorg-video-qxl-hwe-16.04 xserver-xorg-video-vesa-hwe-16.04
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
ieee-data python-certifi python-chardet python-httplib2 python-jinja2 python-jmespath python-kerberos python-libcloud
python-lockfile python-markupsafe python-netaddr python-openssl python-paramiko python-pyasn1 python-requests python-selinux
python-simplejson python-urllib3 python-xmltodict python-yaml
Suggested packages:
cowsay sshpass python-jinja2-doc python-lockfile-doc ipython python-netaddr-docs python-openssl-doc python-openssl-dbg python-gssapi
python-socks python-ntlm
Recommended packages:
python-winrm
The following NEW packages will be installed:
ansible ieee-data python-certifi python-chardet python-httplib2 python-jinja2 python-jmespath python-kerberos python-libcloud
python-lockfile python-markupsafe python-netaddr python-openssl python-paramiko python-pyasn1 python-requests python-selinux
python-simplejson python-urllib3 python-xmltodict python-yaml
0 upgraded, 21 newly installed, 0 to remove and 0 not upgraded.
Need to get 7,276 kB of archives.
After this operation, 57.8 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-markupsafe amd64 1.0-1build1 [13.0 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-jinja2 all 2.10-1 [94.6 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-pyasn1 all 0.4.2-3 [46.7 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 python-paramiko all 2.0.0-1ubuntu1.2 [110 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-yaml amd64 3.12-1build2 [115 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-httplib2 all 0.9.2+dfsg-1 [34.6 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic/main amd64 ieee-data all 20180204.1 [1,539 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-netaddr all 0.7.19-1 [213 kB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 ansible all 2.5.1+dfsg-1 [3,197 kB]
Get:10 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-certifi all 2018.1.18-2 [144 kB]
Get:11 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-chardet all 3.0.4-1 [80.3 kB]
Get:12 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-jmespath all 0.9.3-1ubuntu1 [21.2 kB]
Get:13 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 python-kerberos amd64 1.1.14-1 [22.5 kB]
Get:14 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-urllib3 all 1.22-1 [85.1 kB]
Get:15 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 python-requests all 2.18.4-2ubuntu0.1 [58.5 kB]
Get:16 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-lockfile all 1:0.12.2-2 [14.6 kB]
Get:17 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-simplejson amd64 3.13.2-1 [61.2 kB]
Get:18 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 python-libcloud all 2.2.1-1 [1,235 kB]
Get:19 http://mirrors.aliyun.com/ubuntu bionic/main amd64 python-openssl all 17.5.0-1ubuntu1 [41.3 kB]
Get:20 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 python-selinux amd64 2.7-2build2 [138 kB]
Get:21 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 python-xmltodict all 0.11.0-1 [10.3 kB]
Fetched 7,276 kB in 29s (250 kB/s)
Selecting previously unselected package python-markupsafe.
(Reading database ... 212369 files and directories currently installed.)
Preparing to unpack .../00-python-markupsafe_1.0-1build1_amd64.deb ...
Unpacking python-markupsafe (1.0-1build1) ...
Selecting previously unselected package python-jinja2.
Preparing to unpack .../01-python-jinja2_2.10-1_all.deb ...
Unpacking python-jinja2 (2.10-1) ...
Selecting previously unselected package python-pyasn1.
Preparing to unpack .../02-python-pyasn1_0.4.2-3_all.deb ...
Unpacking python-pyasn1 (0.4.2-3) ...
Selecting previously unselected package python-paramiko.
Preparing to unpack .../03-python-paramiko_2.0.0-1ubuntu1.2_all.deb ...
Unpacking python-paramiko (2.0.0-1ubuntu1.2) ...
Selecting previously unselected package python-yaml.
Preparing to unpack .../04-python-yaml_3.12-1build2_amd64.deb ...
Unpacking python-yaml (3.12-1build2) ...
Selecting previously unselected package python-httplib2.
Preparing to unpack .../05-python-httplib2_0.9.2+dfsg-1_all.deb ...
Unpacking python-httplib2 (0.9.2+dfsg-1) ...
Selecting previously unselected package ieee-data.
Preparing to unpack .../06-ieee-data_20180204.1_all.deb ...
Unpacking ieee-data (20180204.1) ...
Selecting previously unselected package python-netaddr.
Preparing to unpack .../07-python-netaddr_0.7.19-1_all.deb ...
Unpacking python-netaddr (0.7.19-1) ...
Selecting previously unselected package ansible.
Preparing to unpack .../08-ansible_2.5.1+dfsg-1_all.deb ...
Unpacking ansible (2.5.1+dfsg-1) ...
Selecting previously unselected package python-certifi.
Preparing to unpack .../09-python-certifi_2018.1.18-2_all.deb ...
Unpacking python-certifi (2018.1.18-2) ...
Selecting previously unselected package python-chardet.
Preparing to unpack .../10-python-chardet_3.0.4-1_all.deb ...
Unpacking python-chardet (3.0.4-1) ...
Selecting previously unselected package python-jmespath.
Preparing to unpack .../11-python-jmespath_0.9.3-1ubuntu1_all.deb ...
Unpacking python-jmespath (0.9.3-1ubuntu1) ...
Selecting previously unselected package python-kerberos.
Preparing to unpack .../12-python-kerberos_1.1.14-1_amd64.deb ...
Unpacking python-kerberos (1.1.14-1) ...
Selecting previously unselected package python-urllib3.
Preparing to unpack .../13-python-urllib3_1.22-1_all.deb ...
Unpacking python-urllib3 (1.22-1) ...
Selecting previously unselected package python-requests.
Preparing to unpack .../14-python-requests_2.18.4-2ubuntu0.1_all.deb ...
Unpacking python-requests (2.18.4-2ubuntu0.1) ...
Selecting previously unselected package python-lockfile.
Preparing to unpack .../15-python-lockfile_1%3a0.12.2-2_all.deb ...
Unpacking python-lockfile (1:0.12.2-2) ...
Selecting previously unselected package python-simplejson.
Preparing to unpack .../16-python-simplejson_3.13.2-1_amd64.deb ...
Unpacking python-simplejson (3.13.2-1) ...
Selecting previously unselected package python-libcloud.
Preparing to unpack .../17-python-libcloud_2.2.1-1_all.deb ...
Unpacking python-libcloud (2.2.1-1) ...
Selecting previously unselected package python-openssl.
Preparing to unpack .../18-python-openssl_17.5.0-1ubuntu1_all.deb ...
Unpacking python-openssl (17.5.0-1ubuntu1) ...
Selecting previously unselected package python-selinux.
Preparing to unpack .../19-python-selinux_2.7-2build2_amd64.deb ...
Unpacking python-selinux (2.7-2build2) ...
Selecting previously unselected package python-xmltodict.
Preparing to unpack .../20-python-xmltodict_0.11.0-1_all.deb ...
Unpacking python-xmltodict (0.11.0-1) ...
Setting up python-simplejson (3.13.2-1) ...
Setting up python-urllib3 (1.22-1) ...
Setting up python-yaml (3.12-1build2) ...
Setting up python-chardet (3.0.4-1) ...
Setting up ieee-data (20180204.1) ...
Setting up python-openssl (17.5.0-1ubuntu1) ...
Setting up python-pyasn1 (0.4.2-3) ...
Setting up python-netaddr (0.7.19-1) ...
Setting up python-xmltodict (0.11.0-1) ...
Setting up python-jmespath (0.9.3-1ubuntu1) ...
Setting up python-certifi (2018.1.18-2) ...
Setting up python-kerberos (1.1.14-1) ...
Setting up python-markupsafe (1.0-1build1) ...
Setting up python-httplib2 (0.9.2+dfsg-1) ...
Setting up python-paramiko (2.0.0-1ubuntu1.2) ...
Setting up python-selinux (2.7-2build2) ...
Setting up python-requests (2.18.4-2ubuntu0.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python-lockfile (1:0.12.2-2) ...
Setting up python-jinja2 (2.10-1) ...
Setting up ansible (2.5.1+dfsg-1) ...
Setting up python-libcloud (2.2.1-1) ...
Then you can check if the Ansible is installed properly, type the following command:
$ ansible --version
Outputs:
devops@devops-osetc:~$ ansible --version ansible 2.5.1 config file = /etc/ansible/ansible.cfg configured module search path = [u'/home/devops/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
You should see that the current ansible version is 2.5.1 from the above ouputs.
Install Ansible from Ubuntu PPA Repository
If you are using the Ubuntu 16.04 Linux, you can use the PPA repository to install the Ansible package, as it is not available in Ubuntu 16.04 repository. If you want to download the latest version of Ansible, you need to add the project’s PPA to your Ubuntu system. Before doing this, you need to first update you package index and install the latest software-properties-common package. Then You can add the PPA repository. So you can add the PPA repository by using the following command:
$ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo apt-add-repository --yes --update ppa:ansible/ansible $ sudo apt update $ sudo apt-get install ansible -y
Or you can do the following steps to install Ansible via PPA repository:
#1 Importing the Ansible signig keys, type:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
Outputs:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
Executing: /tmp/apt-key-gpghome.qaCmAryJ6P/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
gpg: key 93C4A3FD7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg: imported: 1
#2 You can configure the PPA repository using the following command,type:
$ sudo apt-add-repository "deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main"
#3 then you can install the Ansible software package, type:
$ sudo apt install ansible -y
#4 after installed, you can use the following command to check the current Ansible version, type:
$ ansible --version
Install Ansible from a Source Package
If you want to install the latest version of Ansible package on your Ubuntu Linux server, you can try to install it from the Ansible’s git source repository. Just do the following steps:
#1 clone the Ansible git repository to install it from source code, type:
$ git clone https://github.com/ansible/ansible.git --recursive $ cd ./ansible
Outputs:
devops@devops-osetc:~$ git clone https://github.com/ansible/ansible.git --recursive Cloning into 'ansible'... remote: Enumerating objects: 239, done. remote: Counting objects: 100% (239/239), done. remote: Compressing objects: 100% (207/207), done. remote: Total 398620 (delta 163), reused 38 (delta 31), pack-reused 398381 Receiving objects: 100% (398620/398620), 140.73 MiB | 343.00 KiB/s, done. Resolving deltas: 100% (257750/257750), done. Checking out files: 100% (12684/12684), done. devops@devops-osetc:~$ cd ansible/ devops@devops-osetc:~/ansible$ ls bin contrib examples licenses MODULE_GUIDELINES.md requirements.txt test changelogs COPYING hacking Makefile packaging setup.py tox.ini CODING_GUIDELINES.md docs lib MANIFEST.in README.rst shippable.yml devops@devops-osetc:~/ansible$
Once git has cloned the Ansible repository, you can setup the Ansible environment.
#2 List all stable Ansible version that you can checkout, type:
$ git branch -a | grep stable
Outputs:
devops@devops-osetc:~/ansible$ git branch -a | grep stable
remotes/origin/stable-1.9
remotes/origin/stable-2.0
remotes/origin/stable-2.0-network
remotes/origin/stable-2.0.0.1
remotes/origin/stable-2.1
remotes/origin/stable-2.2
remotes/origin/stable-2.3
remotes/origin/stable-2.4
remotes/origin/stable-2.5
remotes/origin/stable-2.6
remotes/origin/stable-2.7
So the latest stable version of Ansible is stable-2.7. Let’s checkout this version of Ansible to your Ubuntu server.
#3 check the latest stable version of Ansible, type:
$ git checkout stable-2.7-2
Outputs:
devops@devops-osetc:~/ansible$ git checkout stable-2.7
Checking out files: 100% (5419/5419), done.
Branch 'stable-2.7' set up to track remote branch 'stable-2.7' from 'origin'.
Switched to a new branch 'stable-2.7'
#4 you can start to compile the Ansible stable 2.7, type:
$ sudo apt install python-setuptools gcc python-dev libffi-dev libssl-dev python-packaging $ make $ sudo make install
#5 Once installed, you can check the version of the Ansible, type:
$ ansible --version
Outputs:
root@devops-osetc:/home/devops# ansible --version ansible 2.7.6.post0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible-2.7.6.post0-py2.7.egg/ansible executable location = /usr/local/bin/ansible python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
So far, you have setup the Ansible properly on your Ubuntu 16.04/18.04 Linux server.
Configure Ansible Host
Ansible will track all of the server hosts and client hosts throught a hosts file. So you need to configure this file before you want to communicate with our other hosts.
Assuming that you have two hosts, one is server host, and another is client host.
server host: 192.168.1.2
client host: 192.168.1.3
You need to open the hosts file under the /etc/ansible directory with sudo privileges, type:
$ sudo nano /etc/ansible/hosts
You should see that all the commented lines in hosts file, and you need to add all of client hosts which you want to contrl or manage.
Adding the following lines:
[servers] client-host ansible_ssh_host=192.168.1.3
Save and close the file when you are finished.
Setup SSH Keys for Remove Ansible Host
Ansible communicates with other hosts using SSH. So you need to create an SSH key on ansible server host, and then copy it to the client host.
On the server host, you can run the following command to generate a SSH key, type:
$ ssh-keygen -t rsa -b 4096
Outputs:
root@devops-osetc:/home/devops# ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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:weM/DbCMeOr6fsRPVBnEt5B36/wb2713MGP0RKwffkA root@devops-osetc The key's randomart image is: +---[RSA 4096]----+ | oo+ . | | . * o E o| | =. + + + | | . +.= . =..| | ..o.S . +.=.| | oo .. o B.+| | .. o o .. *.| | . . . . O| | .++. +B| +----[SHA256]-----+
Then you need to copy the created key to the client host with the following command:
$ ssh-copy-id root@192.168.1.3
Outputs:
root@devops-osetc:/home/devops# ssh-copy-id root@192.168.1.3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.3 (192.168.1.3)' can't be established.
ECDSA key fingerprint is SHA256:tb73WRtga0MyLI69+akAwE/mw09mOX+urv64RoN1/hs.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.3's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.3'"
and check to make sure that only the key(s) you wanted were added.
Or you can copy the content of SSH publick key called /root/.ssh/id_rsa.pub to the remote client host. Just do the following steps:
#1 copy the content of the SSH publick key in server host. Type:
$ cat /root/.ssh/id_rsa.pub
#2 connect to your client host using SSH
$ ssh root@192.168.1.3
#3 paste the SSH key into the file called authorized_keys under /root/.ssh/.
# nano /root/.ssh/authorized_keys
Pasting the content in the clipboard into this file, then save and close the file when you are finished.
Next, try to connect to the client host with SSH to check if the authentication works well or not. Type:
# ssh root@192.168.1.3
You should not be prompted for a password while connecting to the remote client host with SSH.
Testing Ansible
Once both SSH key and hosts file are configured properly, you can try to use an Ansible command to check the Ansible. Now you can use the Ansible command to connect to your hosts defined in /etc/ansible/hosts file to configure or check the remote client host.
You can ping all of the client hosts with the following command:
# ansible -m ping all
Outputs:
root@devops-osetc:/home/devops# ansible -m ping all client-host | SUCCESS => { "changed": false, "ping": "pong" }
If the client host is up, you should see the above output. And “all” in the above command means that the ansible command will execute ping command on all the client hosts.
You can also execute the ping command on a specified client host, type:
# ansible -m ping client-host
Outputs:
root@devops-osetc:/home/devops# ansible -m ping client-host client-host | SUCCESS => { "changed": false, "ping": "pong" }
If you want to get the memory useage on the client host, type the following ansible command:
# ansible -m shell -a 'free -m' client-host
Outputs:
root@devops-osetc:/home/devops# ansible -m shell -a 'free -m' client-host client-host | CHANGED | rc=0 >> total used free shared buff/cache available Mem: 1838 132 67 300 1638 1332 Swap: 1499 16 1483
Note: the “shell” module used to send a terminal command to the remote client host and retrieve the results.
Conclusion
You have installed and configured Ansible on your Ubuntu 18.04 Linux server successfully. And you can configure or manage the remote client hosts throught the ansible server host.
If you want to learn more about the Ansible tool, you can go to the official Ansible site to checkout the latest Ansible document.