آموزش نصب Ansible در ubuntu

Ansible is a free and open source software that can be used to automate software provisioning, configuration management, and application deployment. Unlike Puppet or Chef, you don’t have to set up a client-server environment before using Ansible. You can manage multiple hosts from a central location. This tool is very simple yet powerful to automate complex multi-tier IT application environments. Ansible communicates over normal SSH channels to retrieve information from remote machines and perform tasks.

In this tutorial, we will learn how to install and use Ansible on Ubuntu 18.04 server.

Requirements

  • Two Ubuntu 18.04 server system with OpenSSH server installed.
  • A static IP address 192.168.0.101 is set up on the server system and 192.168.0.104 is set up on the client system.
  • A non-root user with sudo privileges is set up on both systems.

Getting Started

Before starting, update your system with the latest version with the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Once your system is up-to-date, restart your system to apply the changes.

Install and Configure Ansible

By default, The latest version of Ansible is not available in the Ubuntu 18.04 default repository. So you will need to add Ansible PPA to your server. You can do this by running the following command:

sudo apt-add-repository ppa:ansible/ansible

Next, update the repository and install Ansible with the following command:

sudo apt-get update -y
sudo apt-get install ansible -y

Once the installation is completed, you can check the Ansible version with the following command:

sudo ansible --version

Output:

ansible 2.7.1
 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/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]

Next, you will need to define your client system which you want to manage in Ansible hosts file. You can do this by editing /etc/ansible/hosts file:

sudo nano /etc/ansible/hosts

Add the following lines:

[Client]
node1 ansible_ssh_host=192.168.0.104

Save and close the file, when you are finished.

Configure SSH Keys for Client System

Ansible uses SSH to communicate with Client host. So you will need to configure key-based ssh authentication for Client host.

First, generate an SSH key pair with the following command:

ssh-keygen

Output:

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:bTVjTCKqxD8rnIw7/6mB6ZH9cnhzzOFg+/+x4clSOow [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| . . . |
| . . . + |
| o . * |
| . o . o o |
| . o S o |
| O .oo.. . |
| * Bo.* + oo |
| ..oo+=.E =o = |
| ooo*++...+* |
+----[SHA256]-----+

Next, copy this public key to the Client system with the following command:

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Output:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

Working with Ansible

Ansible is now installed and configured. It’s time test Ansible.

On the Ansible server, try to ping your Client system using Ansible with the following command.

ansible -m ping Client

Output:

node1 | SUCCESS => {
 "changed": false, 
 "ping": "pong"
}

If you have multiple client configured in your Ansible hosts file. Then, you can ping all the Client system with the following command:

ansible -m ping all

You can check the status of the Apache web server on the Client system with the following command:

ansible -m shell -a 'service apache2 status' Client

Output:

node1 | CHANGED | rc=0 >>
 * apache2 is running

To check the partition size of the Client system, run the following command:

ansible -m shell -a 'df -h' Client

Output:

node1 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 138G 48G 83G 37% /
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 67M 1.9G 4% /run/shm
none 100M 36K 100M 1% /run/user
/dev/sda5 225G 35G 180G 16% /Data

Congratulations! you have successfully installed and configured Ansible on Ubuntu 18.04 server. You can now easily use Ansible to execute simple tasks remotely

نوشته های مشابه