Installing and using Ansible on Ubuntu 16.04

** Ansible ** is a free and open source configuration management system that allows you to deploy and manage your applications.

Alibaba Cloud Tech Share Author, Hitesh Jethva.

Ansible is a free open source configuration management system that allows you to automatically deploy and centrally manage your applications. With Ansible, you can easily manage and control a large number of servers from a central location. Ansible is a great alternative to Chef and Puppet because it has much less overhead to get started. Ansible uses SSH channels to get information from remote systems, so no additional software needs to be installed on the client computer.

This tutorial will show you step by step how to install and use Ansible on Alibaba Cloud ECS (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) Ubuntu 16.04 server.

Prerequisites

--I have two Alibaba Cloud ECS instances with Ubuntu 16.04 installed. --The server node has a static IP address of 192.168.0.103. --The client node has a static IP address of 192.168.0.104. --A root password is set for each node.

Launch an Alibaba Cloud ECS instance

First, log in to https://ecs.console.aliyun.com/?spm=a3c0i.o25424en.a3.13.388d499ep38szx"> Alibaba Cloud ECS Console. You will be redirected to the main dashboard. Appropriate You need to select a region and go to ECS Instances (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5). In this tutorial, you are creating an ECS Instance (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) in the Singapore region.

image.png

If you haven't created an instance, check this tutorial (https://www.alibabacloud.com/blog/3-ways-to-set-up-a-linux-server-on-alibaba-cloud_572384?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) or follow the steps in this Quick Start Guide (https://www.alibabacloud.com/help/doc-detail/25422.htm?spm=a2c65.11461447.0.0.530e1dc7SjDKy5). If possible, you should be running two ECS instances (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) on the same region.

Note that I have the SSH key pair set to ECS Instance (https://www.alibabacloud.com/product/ecs?spm=a2c65.11461447.0.0.530e1dc7SjDKy5) as my credentials. This allows you to connect to your instance using SSH.

Install Ansible

By default, Ansible is not available in Ubuntu 16.04 repositories. Therefore, you need to add Ansible's personal repository on the server node. You can add the repository with the following command.

apt-add-repository ppa:ansible/ansible

You should see output similar to the following.

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

http://ansible.com/
 More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpiylu9n1t/secring.gpg' created
gpg: keyring `/tmp/tmpiylu9n1t/pubring.gpg' created
gpg: requesting key 7BB9C367 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpiylu9n1t/trustdb.gpg: trustdb created
gpg: key 7BB9C367: public key "Launchpad PPA for Ansible, Inc." imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

After adding the Ansible repository, you need to update your system so that the repository is added to your system. Run the following command to update your system.

apt-get update -y

After the repository is updated, install Ansible with the following command.

apt-get install ansible -y

Ansible host settings

Ansible uses the hosts file to keep track of all server and client nodes. Therefore, you need to configure this file before communicating with other nodes.

First, open the file with the following command.

nano /etc/ansible/hosts

You should see all the comment lines. Here you need to add all the client nodes you want to manage as follows:

[servers]
client-node ansible_ssh_host=192.168.0.104

Close the file when you are finished saving.

Set up an SSH key for the remote host

Ansible uses SSH to communicate with other nodes. Therefore, you need to generate an SSH key on the server node and copy the SSH key to the client node.

On the server node, execute the following command to generate an SSH key.

ssh-keygen -t rsa -b 4096

You should see output similar to the following.

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:3FvzXmI3EhW7idSZy+ITzW7UrefZP/R5AYx2VBu3ytU root@server-node
The key's randomart image is:
+---[RSA 4096]----+
|              .+.|
|             .. @|
|            +. XE|
|       . . oo+O *|
|        S o +B.Oo|
|           o.o*+ |
|          .  +*+B|
|             o+O*|
|              ..B|
+----[SHA256]-----+

Next, copy the key created by the following command to the client node.

ssh-copy-id [email protected]

Next, perform ssh key authentication on the client node and check if the authentication is successful.

ssh [email protected]

If set correctly, you will not be prompted for a password.

Ansible test

Once everything is set up correctly, it's time to test Ansible.

First, run the following command to ping all the client nodes specified in the hosts file.

ansible -m ping all

If the client node is up, you should see output similar to the following:

client-node | SUCCESS => {
    "changed": false, 
    "failed": false, 
    "ping": "pong"
}

Note: "all" means that if you specify multiple hosts in the hosts file, the above command will ping all hosts.

You can also ping a single host or host group with the following command:

ansible -m ping client-node
ansible -m ping servers

If you want to know the memory usage of the client node, execute the following command.

ansible -m shell -a 'free -m' client-node

You should see output similar to the following.

client-node | SUCCESS | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:          3835       3185        649        177         77        929
-/+ buffers/cache:       2179       1656
Swap:        10793          0      10793

To check the partition size of the client node, run the following command.

ansible -m shell -a 'df -h' client-node

Output.

client-node | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda4        92G   27G   61G  31% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           384M  1.2M  383M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G  1.9M  1.9G   1% /run/shm
none            100M   44K  100M   1% /run/user
/dev/sda5       353G   75G  261G  23% /Data

Conclusion

Congratulations. Succeeded in setting Ansible in Alibaba Cloud ECS Server. Now you can easily manage multiple servers by running a single command from a central location.

Recommended Posts

Installing and using Ansible on Ubuntu 16.04
Using Flutter on Ubuntu (Part 2)
Installing OpenMX on Ubuntu 18.04.5 LTS
I tried using YOLO v4 on Ubuntu and ROS
Using Azure IOT Hub on Ubuntu 20.10.
Install JDK and JRE on Ubuntu 16.10
Install ngrok on ubuntu16.04 using Vagrant
Enable Java 8 and Java 11 SDKs on Ubuntu
Install Docker on Ubuntu and set up remote connection using tls
Installing and configuring ClipBucket and Nginx on CentOS 7
Instructions for installing and using the AWS command line (awscli) on CentOS
The story of installing raspi-config on ubuntu 18.04 and changing the initial settings of GPIO
tmux on Ubuntu
Install java and android-sdk on Mac using homebrew
Build and install Wireshark Development Release (3.3.1) on Ubuntu
Publish MySQL externally and log in on Ubuntu
Protobuf and gRPC C ++ environment construction on Ubuntu 18.04
Android application development using Unity + ARCore on Ubuntu
Simple installation of nginx and Docker using ansible
Build a DHCP and NAT router on Ubuntu 16.04
Install and switch between multiple Javas on Ubuntu
Docker on Ubuntu18.04 on WSL2 and VSCode installation instructions
On ubuntu, scilab, octave and R, sympy, etc.
Creating an SSL certificate using Let's Encrypt and setting up Nginx on Ubuntu 20
Screen recording on Ubuntu 20.04
Web Bluetooth on Ubuntu20.04
Installing Visual Studio Code on Ubuntu 20.04 (automatic update support)
Try DisplayLink on Ubuntu 20.04
Reinstall Kubernetes on Ubuntu 19.10
I built an Ubuntu environment on Windows 10 using WSL2.
Use Flutter on Ubuntu
Install pyqt5 on ubuntu
Easily build Redmine on Windows using WSL2 and Docker
Using JDBC on Linux
Install Ruby on Ubuntu 20.04
Install java and maven using brew on new mac
Setting JAVA_HOME on Ubuntu
Install Autoware on Ubuntu 18.04.5
Put JetBrains on Ubuntu
Use mkdir on ubuntu
Install rbenv with apt on ubuntu and put ruby
Apache2 on Ubuntu20.04 LTS
ubuntu on wsl part 10
Install Homebrew on Ubuntu 20.04
Run tiscamera on Ubuntu 18.04
Build Zabbix on Ubuntu 20.04
Talk about introducing Ubuntu 20.04 on Windows 10 and text editor
How to Install Elixir and Phoenix Framework on Ubuntu 20.04 LTS
Pit pits when installing dotnet on Ubuntu 18.04 LTS + WINE + winetricks
Set up Django on Ubuntu 16.04 with PostgreSQL and Gunicorn on ECS
Configuration script for using docker in proxy environment on ubuntu 20.04.1
[Ruby on Rails] Infinite scrolling using gem kaminari and jscroll
Create SSL certificate on Ubuntu 18.04
Building WebGIS on Ubuntu20.04 LTS
Install OpenJDK7 (JAVA) on ubuntu 14.04
Build VNC Server on Ubuntu 20.04
Install Cybozu Office 10 on Ubuntu 20.4
Extend swap area on Ubuntu 18.04
Install Docker on Ubuntu Server 20.04
Ubuntu on Windows Community Preview
Japanese input on Ubuntu20.04 Desktop