Learning Ansible : PART 3

Services: apache2_module, handlers, notify:


apache2_module: Enables or disables a specified module of the Apache2 webserver.


Handler are tasks that are defined in a playbook and can be called by the notify property in case of state change during task execution.



[root@licvdo1000 ansible]# cat webcomponents.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install web components
       yum: name={{item}} state=latest update_cache=yes
       with_items:
       - httpd
       - mod_wsgi
       - python2-pip
       - python-virtualenv
     - name: ensure httpd is running
       service: name=httpd state=started enabled=yes
     - name: ensure mod_wsgi enabled
       apache2_module: state=present name=wsgi
       notify: restart httpd

   handlers:
       - name: restart httpd
         service: name=httpd state=restarted



[root@licvdo1000 ansible]# ansible-playbook webcomponents.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install web components] **********************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of
 using a loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['httpd',
'mod_wsgi', 'python2-pip', 'python-virtualenv']` and remove the loop. This feature will be removed in version
2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [licvdo1001] => (item=[u'httpd', u'mod_wsgi', u'python2-pip', u'python-virtualenv'])

TASK [ensure httpd is running] *********************************************************************************
ok: [licvdo1001]

TASK [ensure mod_wsgi enabled] *********************************************************************************
ok: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Copy Module:


It is used to copy a file from the control machine to the target machine. 


[root@licvdo1000 ansible]# cat webserver.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install webserver
       yum: name=httpd state=latest update_cache=yes
     - name: ensure httpd is running
       service: name=httpd state=started enabled=yes
     - name: copy demo html file
       copy: src=index.html dest=/var/www/html/index.html mode=0755
       notify: restart httpd

   handlers:
       - name: restart httpd
         service: name=httpd state=restarted


Before running the playbook, the URL is displaying the below webpage:



Now, We run this playbook and when We try to open the same URL, it should show the new page which is copied to the destination.

[root@licvdo1000 ansible]# ansible-playbook webserver.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install webserver] ***************************************************************************************
ok: [licvdo1001]

TASK [ensure httpd is running] *********************************************************************************
ok: [licvdo1001]

TASK [copy demo html file] *************************************************************************************
changed: [licvdo1001]

RUNNING HANDLER [restart httpd] ********************************************************************************
changed: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



Boom!!! We can see the new webpage which we have created.








Learning Ansible: PART 2

Writing and Executing Playbooks [Contd..]


There are several modules are available in ansible documentation. Please refer the below link and try to understand at-least the popular ones.

https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

Installing a package on testserver group

[root@licvdo1000 ansible]# cat testserver.yml
---
- hosts: testserver
  tasks:
   - name: install nginx
     yum: name=nginx state=present update_cache=yes


[root@licvdo1001 ~]# rpm -q nginx
package nginx is not installed

[root@licvdo1000 ansible]# ansible-playbook testserver.yml

PLAY [testserver] ********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [licvdo1001]

TASK [install nginx] *****************************************************************************************************************************************

changed: [licvdo1001]

PLAY RECAP ***************************************************************************************************************************************************
licvdo1001                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


[root@licvdo1001 ~]# rpm -q nginx
nginx-1.12.2-3.el7.x86_64


Using jinja in your playbooks: {{with_items}}


Note: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['pkg1', 'pkg2', 'pkg3']` and remove the loop. This feature will be removed in version 2.11.

Here "with_items" is used to refer multiple package names in a single "yum" statement. It can be referred as loop as we use in conventional programming languages.

[root@licvdo1000 ansible]# cat webcomponents.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install web components
       yum: name={{item}} state=latest update_cache=yes
       with_items:
       - httpd
       - python2-pip
       - python-virtualenv



[root@licvdo1000 ansible]# ansible-playbook webcomponents.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install web components] **********************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of
 using a loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['httpd', 'python-
pip', 'python-virtualenv']` and remove the loop. This feature will be removed in version 2.11. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [licvdo1001] => (item=[u'httpd', u'python-pip', u'python-virtualenv'])

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Use of Service Module in Ansible playbook:


[root@licvdo1000 ansible]# cat webcomponents.yml
---
 - hosts: testserver
   become: true
   tasks:
     - name: install web components
       yum: name={{item}} state=latest update_cache=yes
       with_items:
       - httpd
       - python2-pip
       - python-virtualenv
     - name: ensure httpd is running
       service: name=httpd state=started enabled=yes
[root@licvdo1000 ansible]# ansible-playbook webcomponents.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install web components] **********************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of
 using a loop to supply multiple items and specifying `name: "{{item}}"`, please use `name: ['httpd',
'python2-pip', 'python-virtualenv']` and remove the loop. This feature will be removed in version 2.11.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [licvdo1001] => (item=[u'httpd', u'python2-pip', u'python-virtualenv'])

TASK [ensure httpd is running] *********************************************************************************
ok: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


[root@licvdo1000 ansible]# cat loadbalancer.yml
---
- hosts: testserver
  tasks:
   - name: install nginx
     yum: name=nginx state=present update_cache=yes
   - name: ensure nginx gets started and enabled
     service: name=nginx state=started enabled=yes

[root@licvdo1000 ansible]# ansible-playbook loadbalancer.yml

PLAY [testserver] **********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [licvdo1001]

TASK [install nginx] *******************************************************************************************
ok: [licvdo1001]

TASK [ensure nginx getss started and enabled] ******************************************************************
changed: [licvdo1001]

PLAY RECAP *****************************************************************************************************
licvdo1001                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@licvdo1000 ansible]#


Note: Earlier it failed with error "unable to start nginx.service, failed to bind port 80", as the TCP port  80 is occupied by httpd. So, for nginx I have changed the default port to 81 from 80 and boom.. it's started.


Learning Ansible: PART 1


Installing Ansible:



[root@licvdo1000 ~]# yum install ansible
[root@licvdo1000 ~]# ansible --version
ansible 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#
[root@licvdo1000 ~]#

Ansible Version Check:


[root@licvdo1000 ~]# ansible-playbook --version
ansible-playbook 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]
[root@licvdo1000 ~]# ansible-galaxy --version
ansible-galaxy 2.8.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-galaxy
  python version = 2.7.5 (default, Nov  1 2018, 03:12:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)]

Default Ansible Configuration:


[root@licvdo1000 ansible]# pwd
/etc/ansible
[root@licvdo1000 ansible]# more ansible.cfg
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False


[root@licvdo1000 ansible]# more hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

Creating your own configuration:


[root@licvdo1000 ansible]# pwd
/home/f776xb6/ansible
[root@licvdo1000 ansible]# ll
total 8
-rw-r--r--. 1 root root 59 Aug 16 12:04 ansible.cfg
-rw-r--r--. 1 root root 71 Aug 16 12:30 hosts
[root@licvdo1000 ansible]#

[root@licvdo1000 ansible]# cat ansible.cfg
[defaults]

inventory      = /home/f776xb6/ansible/hosts

[root@licvdo1000 ansible]# cat hosts
[testserver]
licvdo1001

[control]
licvdo1000 ansible_connection=local
[root@licvdo1000 ansible]#


Listing hosts from inventory and using wild characters:


[root@licvdo1000 ansible]# ansible --list-hosts all
  hosts (2):
    licvdo1000
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts '*'
  hosts (2):
    licvdo1000
    licvdo1001
[root@licvdo1000 ansible]# pwd
/home/f776xb6/ansible
[root@licvdo1000 ansible]# ansible --list-hosts testserver
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts licvdo1001
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts test*
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts testserver:control
  hosts (2):
    licvdo1001
    licvdo1000
[root@licvdo1000 ansible]# ansible --list-hosts testserver,control
  hosts (2):
    licvdo1001
    licvdo1000
[root@licvdo1000 ansible]# ansible --list-hosts testserver[1]
 [WARNING]: No hosts matched, nothing to do

  hosts (0):
[root@licvdo1000 ansible]# ansible --list-hosts testserver[0]
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts \!control
  hosts (1):
    licvdo1001
[root@licvdo1000 ansible]# ansible --list-hosts \!test*
  hosts (1):
    licvdo1000
[root@licvdo1000 ansible]#

Running Ad-hoc commands:


[root@licvdo1000 ansible]# ansible -m ping all
 [WARNING]: Platform linux on host licvdo1000 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
 [WARNING]: Platform linux on host licvdo1001 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@licvdo1000 ansible]# ansible -m command -a "hostname" all
 [WARNING]: Platform linux on host licvdo1000 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | CHANGED | rc=0 >>
licvdo1000

 [WARNING]: Platform linux on host licvdo1001 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | CHANGED | rc=0 >>
licvdo1001

[root@licvdo1000 ansible]#


#### command is the default module ###############


[root@licvdo1000 ansible]# ansible -a "hostname" all
 [WARNING]: Platform linux on host licvdo1000 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1000 | CHANGED | rc=0 >>
licvdo1000

 [WARNING]: Platform linux on host licvdo1001 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information.

licvdo1001 | CHANGED | rc=0 >>
licvdo1001

[root@licvdo1000 ansible]#


Writing and executing playbooks:


[root@licvdo1000 ansible]# cat hostname.yml
---
 - hosts: all
   tasks:
     - name : get server hostname
       command: hostname


[root@licvdo1000 ansible]# ansible-playbook hostname.yml

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [licvdo1000]
ok: [licvdo1001]

TASK [get server hostname] ***********************************************************************************************************************************
changed: [licvdo1000]
changed: [licvdo1001]

PLAY RECAP ***************************************************************************************************************************************************
licvdo1000                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
licvdo1001                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Installation of Jenkins on Linux and Deployment NGINX through Jenkins

Installation of Jenkins: [root@worker1 ~]# useradd -c "jenkins user" jenkins [root@worker1 ~]# passwd jenkins Changing passw...