Ansible -- UDMY
+++++++++++++++++++++++++++++++++++++++++++++++++++
ANSIBLE :
Credit : James Spurin
This documentation is made for my reference from the video creator named : Named James Spurtin.
So every thing and anything related to Ansible is the contributor mentioned above.
I made this reference note so that I don't have to sit over video again and again and helps me to refresh my learning once again.
+++++++++++++++++++++++++++++++++++++++++++++++++++
Section 1 : Installation and Configuration of Ansible :
Ansible Requirements - Python , PIP , and Virtual Environments .
Ansible can be installed in a verity of different ways. for different OS and the advantages and disadvantages to these operations . We will learn PIP - Python Package Manager and how this could be used in a effective means at a managing and maintaining Ansible releases.
Finally we will learn about Virtual Environments in Python . And how we can make use of them with ansible
Ansible is Automated Framework written in Python . Of coure knowledge of python a benefit when we look into some of the advanced topics of this course . It is not a requirement for lerning and using Ansible.
Pythion 2.6 or 2.7 can be used with Ansible. Python 3 is being actively developed as a specific branch in the main Ansible github repository . At the time of creating this video it is already very stable. However this is still treated as a development branch . This course focuses on the use of Python 2 with the deruns wherever possible between Python 2 and Python 3 , where it involves development in Python . If you do wish to use python 3 with this course most I expect you will be sucessful with the use of the content . It is work in progress . Your knowledge may vary .
There are variety of ways in which Ansible can be installed.
Each with different Merits . The following slide summaries a free common approaches
Python VM: In python a VM is an isolated area in which packages can be installed . They are created using an existing python installation. This could be version 2 or version 3 . The virtual environment will relate to the version of Python that was used to built it. You can create multiple virtual environments.
When the virtual environments are activated , when we install a package . It will install into a virtual environment for example if we use PIP . Issue the command
$ pip install ansible
This will install a cub version of Ansible available from PIPE PI pacjage Index into the virtual environment . if we want to install a specific version for example an earlier version of Ansible 2 . we can give this specific package of Ansible to PIP . We can also do the same if we install a later version.
Virtual environment separate your working environments from the main Python Installation. Providing a verity of advantages . It keeps your working environment clean as all changes are applied to your virtual environment and not on the system that has installed Python. There are also less dependencies using a super user. As virtual environments can be created by non root users. Finally virtual environment provide a convenient means for installing Ansible. In the corresponding videos we are going throigh a process of setting up an environment for the use of Ansible through out this course.
ANSIBLE COURSE : ENVIRONMENT SETUP
In this course we are going to take a look at
1. Oracle Virtual Box
The video sessions make use of 8 hosts .
Oracle Virtual Box is a x86 Hypervisor, allowing you to run VMs on windows, MacOs , Linux and Solaris .
The videos sessions make use of 8 hosts
- Ubuntu C - Is our Ansible Control Host. with -c being added to signify that this the control host
- CentOS 1 , 2 & 3 - Are all CentOS hosts
- Unbunti 1, 2 & 3 - Three Ubuntu hosts
- dnsmasq -- finally there is a host called dnsmasq as the name suggest is been confiured with DNS mask service.This acts as a named server for local hosts and as a forwarding DNS server. It is important to know that this machines acts as DNS server to all of the other machies. You will have to have this machine running for other virtual machines to run as expected,
- The virtual machines can be downloaded from the
URL : https://goo.gl/b68auU
Pause the video and download each machine in the background
The 8 machines are been configured on virtual box using two Network adapters . As shown in the blue and green arrows. Both virtual machine adapter must be bridges with the same network adapter on your system . And that adapter must be providing an IP assignment with an internet connectivity through DHCP . And it is typically used in home internet routers to automatically provide IP addresses and internet connectivity . If you access the internet on your computer through your WIFI router then the adapter will be bridged in the adapter the two adapters too in your vrtual machine will be this WIFI adapter
If you connect an eithernet cable to this computer and automatically assign an IP and connectivity will be used in this adapter as a bridge to both the adapters in the virtual machines.
Adapter 1 signified by the green arrows has been configured in the system to use DHCP get an IP address , so when this is bridged . DHCP enabled adapter . it should automatically assign an IP address on adapetr1 for each of the instances
Adapter 2 : signified by the blue arrow has been configured with static ip addresses , each of this instance has a 192.168.0.4x with dnsmasq bing 192.168.0.40
As this adapter is also bridged on your main network.
If you were able to set an ip address on your system or create a virtual interface on your system
For all the machine there are these users configured on them
Ansible on Linux :
Running Ansible as a control host on Linux .
Installing Ansible in a Ubuntu machine:
$ sudo apt-get update
$ sudo apt install ansible
installation location is shown by the below command.
$ which ansible
Check the version of the Ansible Installed.
Installing using APT Packages.
what are going to do is use the APT to check for Ansible personal archives.to see this how this improves
We have restored that machines back to its original state . Now the ansible is removed and the system is without ansible. This time we are going to setup personal packages archive.
$ sudo apt-get update
$ sudo apt-get install software-properties-common
Now we add in the APT repository
$ sudo apt-add-repository ppa:ansible/ansible
In comparison to the earlier installation this time you have more details about the installation.
He again restored it back to the earlier state :
Installing Ansible by VM and PIP
$ sudo apt-get update
The we will install the dependencies .
$ sudo apt-get install python-minimal virtualenv python-dev build-essential
we will make a directory to store
$ mkdir ansible
$ cd ansible
we will create a virtual environment .
$ virtualenv venv27
We will not activate our virtual environment
$ source venv27/bin/activate
Check the python installion
$ which python
$ which pip
We are not doing s pip install ansible
Now if you install anything using PIP will be installed in its virtual environment
$ pip install ansible
$ which ansible
This path is relevant to the virtual environment this is no system path
$ ansible --version
One of the advantages of have pip is that we have loads of control over pip.
I am going to uninstall ansible
$ pip uninstall ansible
Checking if git is installed
$ git
we are going to install git
$ sudo apt install git // installed using apt package manager
We can again install using PIP
$ pip install git+https://github.com/ansible/ansible
This can take a while for you are pulling it from Github. internet speed and stuff like that will depend on this installation.
$ ansible --version
Now that covers up our three main approaches of installing ansible on Ubuntu
Now we will see the other approaches of installing Ansible
CentOS system
Installing Ansible by the creating your own RPM
First you are going to install your dependencies
$ sudo yum install asciidoc python-jinja2 PyYAML git python-setuptools rpm-build python2-devel
we are just going to the /tmp directory
$ cd /tmp
and we are going to clone the current version of Ansible.
$ git clone git://gitub.com/ansible/ansible.git
$ cd /ansible // go into the ansible directory that we cloned now.
Now we will create the RPM, Now this will not work. But we will proceed to check how we can solve this.
$ make rpm
This has build our RPM -- however
RPM is installed in RPM
![]() |
$ ls rpm_build
Installing dependencies below
$ sudo rpm -Uvh rpm-build/ansible-2.5.0-100.git201712021324.1faa0a7.devel.e17.centos.noarch.rpm
What we see here is that this is build in with some dependencies . So for us to actually progress with this we need to install certain dependencies .
$ yum install python-paramiko python-six python2-cryptography sshpass
now again run the previous commands.
$ sudo rpm -Uvh rpm-build/ansible-2.5.0-100.git201712021324.1faa0a7.devel.e17.centos.noarch.rpm
$ which ansible
/usr/bin/ansible
$ ansible --version
For centOS
$ sudo yum install epel-release
$ sudo yum install ansible
$ ansible --version
To do this installation we must install , in order to do this is compiler and the corresponding compiler utilities .
$ sudo yum install 'Development Tools'
Install dependencies
$ sudo yum install python-virtualenv libffi-devel openssl-devel python-cffi PyYAML
$ mkdir ansible
$ cd ansible
Create virtual environment
$ virtualenv venv27
$ source venv27/bin/activate
$ which python
/root/ansible/venv27/bin/python
$ which pip
Sometimes in CentOS and RHEL . we have issue installing using PIP.
There is an easy fix for this .
$ pip install --upgrade setuptools
$ pip install ansible
7 : Validating Ansible Installation
We are going to look at ansible configuration shell command to check the os and then execute the script accordingly ion file and how it is going to vary depending on our installation .If you have installed Ansible , if you have configured your own ansible control host - by one of the methods.
- You should have an ansible folder ,
- Within which you should have a sub directory venv27
- If so activate your virtual machine : source venv27/bin/activate
If instead you have decide to go with an alternative approach such as installing Ansible through system package . Please create an ansible directory now. You can continue with out the virtual environment now.
We are going to use the same command to validate our system but this time we are going to work with the remote system and we are going to make use of an Ansible configuration and inventory file. Before we get to that when going for an installation process
The above is by order of precedence
If there was an environment variable called ansible_config . it will attempt to use whatever file name this refers to. The next priority is ./ansible.cfg . The dot represents the current directory. So it will look at the current directory for the file called ansible.cfg . The third priority is ~/.ansible.cfg . Current users home directory
echo ~ -- will show the users path home directory
Last is in the system location of etc : /etc/ansible/ansible.cfg -- You will not be able to write to this location unless you are super user or executing commands in a way that provides super user access,
If we list we have all of these configuration files
There is another default file this is with relations to the hosts file . /etc/ansible/hosts . we are not going to use this approach , instead what will are going to doing is create our own inventory file and within that inventory file we are going to specify the hosts location.
He uses this method, again it is a system user path and would need super user privileges to use it. /etc/ansible/hosts -- we are going to create our own hosts file.
The host file can be structured in a variety of different ways , the most common format is like any file in windows . It can be any YAML or JSON file. From now I am going to use the any format.
vi hosts
[ALL] -- Build in session called
[all]
centos1 -- added my host centos one
Technically here we didnt need to add the [All] GROUP , because all hosts is assigned the all group in ansible
we are going to use a file called ansible.cfg
vim ansible.cfg
Creating a default session.
[default]
inventory = hosts -- we are defining an inventory of hosts.
And lets try to ping the host : At this time it is going to fail but we go on by rectifying the issue as we go.
$ ansible all -m ping
Because there is no SSH that is configure it is unable to make a connection to the host machine.
Once you run this command your ansible controller makes note of the hosts machine in the file below
$ cat /.ssh/known_hosts
Creating a SSH key
$ ssh-keygen -H -F centos1
If we do the same again with the IP address
$ ssh-keygen -H -F 192.168.0.45
If you above and see this careful - the centos1. If we were to remove these entries . Let me just take them out of it.
$ vim ~/.ssh/known_hosts
remove all the entries from here. And again if we re-run .
$ ansible all -m ping
It once again prompts
At the moment it is not ideal from an ansible perspective because every host that we connect to it is going to ask us whether or not you want to establish and continue connecting .
what we can do here is set an environment variable ANSIBLE_HOST_KEY_CHECKING=False . by setting it before the command
$ ANSIBLE_HOST_KEY_CHEKKING=False ansible all -m ping
This failed once again. But this time It has automatically added the host key to the node hosts
Though this is a solution but not an ideal solution.
what we want here is a permanent solution . I will remove it from the ssh key file again
$ ~/.ssh/known_hosts
$ vim ansible.cfg -- lets edit our ansible configuration file.
added
host_key_checking = False
it fails again but it has automatically added that host file
At the moment the remote host doesn't even have a .ssh directory.
If we now have a look at centos1
And with in that is the authorised key fie
And remember what i was saying here about the permissions
And it is working as expected.
Now with the trust relationship in place. If we try our ping module. We can successfully ping
$ ansible all -m ping
I mentioned earlier by default all hosts belong to group all group . To prove this we are going to toggle the host file and remove the [all] from the file
[all] -- removed this from the host file
centos1
And now we re execute the command once again
Before we go any further I want to show you that ansible is flexible in the way it can be used. For reference the equivalent can be done using the command line tool with out an inventory and with out a configuration file.
$ ansible all -i
The -i expects an inventory file but we give a hostname provinding that we end this with a comma
$ ansible all -i centos1, -m ping
This with the exception of the host key checking is equivalent of what we have in the inventory and the ansible configuration file.
The adhoc command line tool can be used to with any of the ansible modules
The ansible "debug" module is used to print statements during its execution.
$ ansible all -m debug
Therefore ansible command line tool accepts arguments and we can pass arguments by
-- args / -A
$ ansible all debug --args='msg="This is a custom debug message"'
Lets use verbosity
If we were to set the debug module parameter for verbosity to 3 , it will only run when ansible is running in a particular verbosity mode. For example;
The output there shows it was skipped
1v -- v -- means verbosity level 1
2 v -- vv -- means verbosity level 2
3v --vvv -- means verbosity level 3
Lets setup other host machines for our lab -- got to hosts file
if we run the ping module against all group now . it is going to succeed for one and fail for all of the others.
We can also do this for individual groups
$ ansible centos -m ping
Again it is going to succeed for one and fail for other two.
You can also refer to the [all] group with '*' sign
$ ansible '*' -m ping
to resolve this we need the ssh keys on all of these hosts machines. And the easiest way for us to do this is to just be with some simple bash loopin here.
Writing a loop script
And now lets check it up
$ ansible all -m ping -o
-o flag reduces the output to a single line .
You can check the list of host that you will be executing the code via ansible command line.
$ ansible centos --list-host
$ ansible centos --list-host
$ ansible all --list-host
Now that we have our environment setup . We can get in to master Ansible.

































































































































Comments
Post a Comment