Install Ansible on Linux
Objective
- Install Ansible on Linux Machines. Use OL 7 Images
Pre-Requisite
- An Oracle Linux 7 VM to install Ansible and the required software.
- Private/Public Key pair already generated on the machine.
- Two local VMs,
tester1
andtester2
. - Use the same ssh public key for all three machines to save time
- Necessary Security Rules to allow ping and
ssh
from each other using hostnames.
Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.
Configuration |
Conroller Node |
Managed Node1 |
Managed Node2 |
hostname |
server.example.com | tester1.example.com | tester2.example.com |
OS |
CentOS 7/OL7 | CentOS 7/OL7 | CentOS 7/OL7 |
IP Address |
10.10.0.100 | 10.10.0.101 | 10.10.0.102 |
rpms required |
Ansible, python3 | python3 | python3 |
Automation and Orchestration
- Automation allows enterprises to gain and maintain speed efficiency via software automation tools.
- Orchestration use automation and executes large workflows systematically.
- In the cloud, orchestration not only deploys an application, but it connects it to the network to enable communication between users and other apps.
- It ensures auto-scaling to initiate in the right order, implementing the correct permissions and security rules.
- Automation makes orchestration easier to execute.
Automation | Orchestration | |
Concept | Tasks or functions which are accomplished without any human intervention. | Arranging and coordination of tasks that are automated to create a consolidated workflow. |
Nature of Tools | Activities occur in an order. They are also required to be granted permissions roles. | Tools can enumerate various resources, IAM roles, instance types, etc., configure them and ensure that there is interoperability between them. |
Role of Personnel | Engineers are required to complete a myriad of manual tasks. | It requires less intervention from personnel. |
Policy decisions | Typically does not implement policy decisions which fall outside of OS-level ACLs. | It handles all permissions and security of automation tasks. |
Resources Used | It uses minimal resources outside of the assigned specific task. | Ensures that cloud resources are efficiently utilized. |
Monitoring and Alerting | Can send data to third party reporting services. | It only involves monitoring and alerting for its workflows. |
Note: There are no separate instructions for installation on tester2. Repeat all commands on tester2 which are executed on tester1
Sequence 1. Create clone of tester1 VM
- Shutdown tester1 VM and Create a clone of the VM.
- Right click on the VM Name and select clone.
- Change the Details as given in the screen shot
- In the next screen keep default and click on clone button
- Once the clone is created, start tester2 and make following changes
- Change the hostname in
/etc/hostname
toexample.com
- Change the IP address in
/etc/sysconfig/network-scripts/ifcfg-enp0s8
to 10.10.0.102
- Remove the UUID record from the same file to avoid any conflict.
- Add an entry into /etc/hosts in all three VMs server, tester1 and tester2
Sequence 2 Install Ansible
- Update /etc/hosts with hostname and IP details of your server and managed hosts.
- Login as root user on your Linux Virtual Machine (server) and create a user “ansible”.
- Create user "ansible" on managed nodes also.
- Since our ansible user would need privilege escalation we will create a new rule for
ansible
user using a new file under/etc/sudoers.d
.
- Create and distribute SSH keys to managed nodes. Enable password less login between our server node and all the managed hosts.
Login or switch user to "ansible" and execute ssh-keygen in the below format. With -P we assign a null password to the key pair.
[ansible@server ~]$ ssh-keygen -t rsa -P ""
- Copy public key to target managed server using ssh-copy-id.
- Also copy the public key on server node. This will also be required.
- Verify password less SSH authentication
The ssh-copy-id command will copy the public key we just created to tester1 and append the content of the key to ansible user's authorized_keys file under ~/.ssh. You can perform a ssh to managed host to make sure you can connect to the server without giving any password or passphrase.
[ansible@server ~]$ ssh tester1- Configure privilege escalation using sudo on Tester Node. Login to tester1 as root user.
Since our ansible user would need privilege escalation we will create a new rule for ansible user using a new file under /etc/sudoers.d on tester node also
[root@tester1 ~]# echo "ansible ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/ansible- Enable developer and EPEL repo on
server
:- Change to Repos Directory
- Edit the this repository file to add Developer and ol7_developer_EPEL repository be adding following lines at the bottom of the file.
- Search for ansible package
- Install
git
andnoarch
rpm onserver
using yum
- Login as user “root” user and do some upgrades:
- Verify the Ansible Version on your server
Sequence 3: Install Python on managed Nodes (tester1 and tester2)
- Install Python on managed nodes. We don't need to install
ansible
on the managed hosts but we must install python3 on managed host. Repeat steps given below on tester1 and tester2