Skip to main content

Introduction to Ansible

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 ~]# hostname
server106.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 ~]# hostname
client205.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

Comments

Post a Comment

Please share your experience.....

Popular posts from this blog

error: db5 error(11) from dbenv->open: Resource temporarily unavailable

If rpm command is not working in your system and it is giving an error message( error: db5 error(11) from dbenv->open: Resource temporarily unavailable ). What is the root cause of this issue? How to fix this issue?   just a single command- [root@localhost rpm]# rpm --rebuilddb Detailed error message- [root@localhost rpm]# rpm -q firefox ^Cerror: db5 error(11) from dbenv->open: Resource temporarily unavailable error: cannot open Packages index using db5 - Resource temporarily unavailable (11) error: cannot open Packages database in /var/lib/rpm ^Cerror: db5 error(11) from dbenv->open: Resource temporarily unavailable error: cannot open Packages database in /var/lib/rpm package firefox is not installed [root@localhost rpm]# RPM manage a database in which it store all information related to packages installed in our system. /var/lib/rpm, this is directory where this information is available. [root@localhost rpm]# cd /var/lib/rpm [root@

Failed to get D-Bus connection: Operation not permitted

" Failed to get D-Bus connection: Operation not permitted " - systemctl command is not working in Docker container. If systemctl command is not working in your container and giving subjected error message then simple solution of this error is, create container with -- privileged option and also provide init file full path  /usr/sbin/init [root@server109 ~]# docker container run -dit --privileged --name systemctl_not_working_centos1 centos:7 /usr/sbin/init For detailed explanation and understanding I am writing more about it, please have look below. If we have a daemon based program(httpd, sshd, jenkins, docker etc.) running inside a container and we would like to start/stop or check status of daemon inside docker then it becomes difficult for us to perform such operations , because by default systemctl and service  commands don't work inside docker. Normally we run below commands to check services status in Linux systems. [root@server109 ~]# systemctl status

AWS cloud automation using Terraform

In this post I'll create multiple resources in AWS cloud using Terraform . Terraform is an infrastructure as code( IAC ) software which can do lots of things but it is superb in cloud automation. To use Terraform we have write code in a high-level configuration language known as Hashicorp Configuration Language , optionally we can write code in JSON as well. I'll create below service using Terraform- 1. Create the key-pair and security group which allow inbound traffic on port 80 and 22 2. Launch EC2 instance. 3. To create EC2 instance use same key and security group which created in step 1 4. Launch Volume(EBS) and mount this volume into /var/www/html directory 5. Upload index.php file and an image on GitHub repository 6. Clone GitHub repository into /var/www/html 7. Create S3 bucket, copy images from GitHub repo into it and set permission to public readable 8 Create a CloudFront use S3 bucket(which contains images) and use the CloudFront URL to update code in /var/w