Introduction to Ansible:-
Ansible is a very intelligent and powerful tool in terms of Configuration management. In very simple language we can say that it is a configuration management automation tool. Apart from that it has capability to do multiple of things in IT world like software provisioning and application-deployment tool enabling infrastructure as code. In terms on platform support, so it can be run on UNIX/Linux system and can configure UNIX/Linux and Windows both kind of operating systems.
From today onward I'll start posting each and everything about Ansible, so you can start an technical journey which will help you to learn Ansible tool from zero to advance level. Since it is very first day so we will try to setup our lab environment and also try to do a simple configuration on a remote host.
I have two RHEL8(Red Hat Enterprise Linux 8) virtual machine installed on Oracle VirtualBox machines. The host on which we install Ansible is know as Ansible controller node, and the hosts which we would like to manage(configure) are known as managed hosts. In my case server106 would be my controller node and client205 would be my managed host.
Controller Code:-
[root@server106 ~]# hostnameserver106.example.com
[root@server106 ~]# ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.9 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::dbc1:598a:4b96:5808 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:e1:60:9e txqueuelen 1000 (Ethernet)
RX packets 21971 bytes 24193459 (23.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14236 bytes 2458565 (2.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Managed Host:-
[root@client205 ~]# hostnameclient205.example.com
[root@client205 ~]# ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.205 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::57f4:618c:1729:b55e prefixlen 64 scopeid 0x20<link>
ether 08:00:27:d9:33:0c txqueuelen 1000 (Ethernet)
RX packets 10085 bytes 11426323 (10.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5014 bytes 446745 (436.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Install Ansible:-
We can install Ansible using multiple ways, like very standard way of installing any package in Linux using yum/dnf but for this you need Red Hat Enterprises subscription or you can install epel-release as a work around. Since I am using RHEL8 hence I am installing epel-release-latest-8.noarch.rpm, you can select as per your OS.
[root@client205 ~]# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
Since Ansible tool is build on Python so we can install it using pip package management tool as well. In my system I have python version 3 hence the command would be pip3.
[root@server106 ~]# python3 -V
Python 3.6.8
[root@server106 ~]# pip3 install ansible
After this if you run ansible --version command and get Ansible version in output and a similer output like below then your installation is successful and you can start using Ansible.
[root@server106 ~]# ansible --version
ansible 2.9.11
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Jan 11 2019, 02:17:16) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
Now lets configure Ansible so that we can use it as an automation tool and configuration management tool.
[root@server106 ~]# mkdir /etc/ansible/
[root@server106 ~]# mkdir /etc/ansible/hosts
[root@server106 ~]# cat >> /etc/ansible/hosts/inventory_hosts.txt
192.168.43.205 ansible_ssh_user=root ansible_ssh_pass=redhat
If you install Ansible using pip3 then it doesn't create it's configuration directories, in my case I installed using pip3 so I have to create these manually. Also don't think that I have mentioned my remote host root password in clear text so Ansible is not a secure tool. It is a very secure tool, but just for very initial demo I have put simple details here.
/etc/ansible/ - Ansible configuration direcotry
/etc/ansible/hosts - Here I'll put my inventory files
/etc/ansible/hosts/inventory_hosts.txt - This is my one of inventory file
However I have added my inventory details inventory_hosts.txt in file but it is not visible to Ansible. So if I search inventory list(the hosts which I would like to manage using Ansible), still it shows there is no inventory list.
[root@server106 ~]# dnf install sshpass -y
[root@server106 ~]# ansible all --list-hosts
[WARNING]: Unable to parse /etc/inventory_hosts.txt as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
hosts (0):
For that we have to mention inventory list file in Ansible configuration file.
[root@server106 ~]# cat >> /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts/inventory_hosts.txt
[root@server106 ~]# ansible all --list-hosts
hosts (1):
192.168.43.205
Now I can see there is a host in ansible inventory, so I can perform operations on it. For simple demo I will install Firefox on managed host. so let's check if firefox is installed on managed host or not?
[root@client205 ~]# rpm -q firefox
package firefox is not installed
Firefox is not installed so let's install firefox on managed hosts using Ansible. If below command output is similar to my out then your ansible setup is perfect and firefox package has been installed on managed host.
[root@server106 ~]# ansible all -m package -a "name=firefox state=present"
192.168.43.205 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: redhat-indexhtml-8-7.el8.noarch",
"Installed: libXxf86vm-1.1.4-9.el8.x86_64",
"Installed: opus-1.3-0.4.beta.el8.x86_64",
"Installed: orc-0.4.28-2.el8.x86_64",
"Installed: rtkit-0.11-19.el8.x86_64",
"Installed: mesa-libgbm-18.3.1-2.el8.x86_64",
"Installed: firefox-60.5.1-1.el8.x86_64",
"Installed: mesa-libglapi-18.3.1-2.el8.x86_64",
"Installed: libICE-1.0.9-13.el8.x86_64",
"Installed: libSM-1.2.3-1.el8.x86_64",
"Installed: libX11-xcb-1.6.7-1.el8.x86_64",
"Installed: libglvnd-1:1.0.1-0.9.git5baa1e5.el8.x86_64",
"Installed: libvisual-1:0.4.0-24.el8.x86_64",
"Installed: pipewire-0.2.5-1.el8.x86_64",
"Installed: alsa-lib-1.1.6-3.el8.x86_64",
"Installed: pipewire-libs-0.2.5-1.el8.x86_64",
"Installed: libglvnd-egl-1:1.0.1-0.9.git5baa1e5.el8.x86_64",
"Installed: libglvnd-gles-1:1.0.1-0.9.git5baa1e5.el8.x86_64",
"Installed: gstreamer1-1.14.0-3.el8.x86_64",
"Installed: libglvnd-glx-1:1.0.1-0.9.git5baa1e5.el8.x86_64",
"Installed: mozilla-filesystem-1.9-18.el8.x86_64",
"Installed: libvorbis-1:1.3.6-1.el8.x86_64",
"Installed: gstreamer1-plugins-base-1.14.0-4.el8.x86_64",
"Installed: startup-notification-0.12-15.el8.x86_64",
"Installed: libwayland-server-1.15.0-1.el8.x86_64",
"Installed: gtk2-2.24.32-4.el8.x86_64",
"Installed: iso-codes-3.79-2.el8.noarch",
"Installed: xml-common-0.6.3-50.el8.noarch",
"Installed: libogg-2:1.3.2-10.el8.x86_64",
"Installed: sbc-1.3-9.el8.x86_64",
"Installed: libpciaccess-0.14-1.el8.x86_64",
"Installed: libdrm-2.4.96-2.el8.x86_64",
"Installed: libtheora-1:1.1.1-21.el8.x86_64",
"Installed: libxshmfence-1.3-2.el8.x86_64",
"Installed: libXt-1.1.5-8.el8.x86_64",
"Installed: xcb-util-0.4.0-10.el8.x86_64",
"Installed: libXv-1.0.11-7.el8.x86_64",
"Installed: mesa-libEGL-18.3.1-2.el8.x86_64",
"Installed: mesa-libGL-18.3.1-2.el8.x86_64"
]
}
If I check firefox package is installed on managed host, then I see that it has been installed successfully.
[root@client205 ~]# rpm -q firefox
firefox-60.5.1-1.el8.x86_64
Ansible Interview Questions Answers
ReplyDelete