Skip to main content

Ansible ad-hoc commands and playbook

In previous post we have understood the usage and basics of Ansible. Also we have have installed Ansible on RHEL8 and setup our Lab environment so that we can practice. Now this is time to move ahead and learn more about Ansible and see how we can use an intelligent configuration management automation tool in our environment. There are two types of automation approaches we can adopt in our environment for configuration management: -

1. Traditional Automation: - Automation using scripts which can be written in any scripting language (Shell, Perl, Python etc.). In this approach as per requirement we write a script and whenever we run this script it do the needful. This kind of automation works fine until there is any change in environment. But as we know this is not possible, our environment keep on changes and slight change may fail our automation completely. How?

Example: - Configure httpd on RHEL6, below are the simple commands we use to configure httpd in RHEL6. Which we can put in syntax of any scripting language and get our work done. Steps: - Install httpd RPM > Configure httpd config > start httpd service.

[root@managed-node ~]# yum install httpd
[root@managed-node ~]# cat >> /var/www/html/index.html
This is my home page
[root@managed-node ~]# service httpd start
[root@managed-node ~]# curl `hostname`
This is my home page

This script will work on all the RHEL6 server and keep on configuring web server but let's suppose there was some requirement and management has decided to upgrade environment on latest Red Hat OS which is RHEL8. Now the problem starts, as we know that to install a package we have dnf command, to start service we have systemctl command in RHEL8. I also know that as of now service and yum commands are there in RHEL8 we can use them but later or sooner these will be removed only dnf and systemctl would be available so considering that service and yum commands are not available. In this scenario the script will fail, it won't work. So we have to ask our developer to update the script according and also it may be that developer don't have experience of RHEL8, so first of all he will learn then he will write the script or he need help of someone who has experience on RHEL8. So finally this kind of issues are very common and no one want to fall into these. To fix this kind of issues we need some intelligent automation approach which works across all the environment. Next approach is made for that only.

 2. Configuration management tools based Automation: - Automation using configuration management tools(Ansible, Puppet, Chef). These are intelligent automation tools. Why I am saying these are intelligent because in these tools we don't have to tell how you have to perform the task? We only have to tell what it has to do?,  Here we will discuss about Ansible. If we continue our previous example of configuration of web server, we just have to tell to Ansible that configure httpd in my environment we never tell how you have to configure httpd. As I mentioned Ansible is an intelligent tool so internally it check on which OS, which version it is and then simply configure it. You don't need to bother about it. This is very simple explanation of why we need configuration management tools like Ansible, Chef and Puppet?

How to start using Ansible: - There are two way of using Ansible - 1. Ad-hoc commands and 2. Playbooks. Ad-hoc commands are easy, quick and good for single task on one or more managed hosts but these are undocumented so can't used again(not reusable). We will start with ad-hoc commands and later on move on playbooks and best practice is also playbooks only. Playbook contain one or more play(task) on one or managed hosts. In simple words playbook is a YAML file in which we write tasks(play) which will be performed on controller nodes. Lets perform some ad-hoc commands and then perform the same using playbook. Make a note I'll perform each step (controller node)ansible.example.comand then verify the same on managed node.

Ad-hoc commands: - 
[root@ansible ~]# ansible all -m package -a "name=httpd state=present"

[root@managed-node ~]# rpm -q httpd
httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64

[root@ansible ~]# mkdir /root/my-ws
[root@ansible ~]# cat /root/my-ws/home_ad-hoc.html
My web server diployed using Ansible Ad-hoc commands!!!
[root@ansible ~]# ansible all -m copy -a "src=/tmp/home_ad-hoc.html dest=/var/www/html/"

[root@managed-node ~]# ll /var/www/html/home_ad-hoc.html
-rw-------. 1 root root 56 Sep  5 16:31 /var/www/html/home_ad-hoc.html

[root@ansible ~]# ansible all -m service -a "name=httpd state=started"

[root@managed-node ~]# systemctl status httpd |grep Active
   Active: active (running) since Sat 2020-09-05 16:33:43 IST; 1min 10s ago
[root@managed-node ~]# curl `hostname`/home_ad-hoc.html
My web server diployed using Ansible Ad-hoc commands!!!

Playbook: - 
[root@ansible ~]# cat > /root/my-ws/home_playbook.html
My web server diployed using Ansible playbook!!!
[root@ansible ~]# vim /root/my-ws/web.yml
- hosts:  192.168.43.72
    tasks:
      - package: -a "name=httpd state=present"
      - copy -a: "src=/tmp/home_ad-hoc.html dest=/var/www/html/"
      - service: -a "name=httpd state=started"

[root@managed-node ~]# systemctl stop httpd
[root@managed-node ~]# dnf remove httpd -y
[root@managed-node ~]# rpm -q httpd
package httpd is not installed

[root@ansible ~]# ansible-playbook web.yml
PLAY [192.168.43.72] *****************************************************
TASK [Gathering Facts] **************************************************
ok: [192.168.43.72]
TASK [package] ********************************************************
changed: [192.168.43.72]
TASK [copy] ***********************************************************
ok: [192.168.43.72]
TASK [service] *********************************************************
changed: [192.168.43.72]
PLAY RECAP ***********************************************************
192.168.43.72     : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@managed-node ~]# curl `hostname`/home_ad-hoc.html
My web server diployed using Ansible Ad-hoc commands!!!

Above output of curl command is enough evidence that whatever we have done is correct and we have successfully deployed web server using Ansible playbook  and web server is working fine.

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