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.


No comments:

Post a Comment

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...