Writing a play book to install multiple packages using vars and then restart services using with_items:
Starting multiple services using with_items:
You can use the with_items parameter to control multiple services in a single task.
Note: Make sure the {{ item }} is within quotes, and the with_items block need to be intended at the level of module name in our case its -here systemd. The following example will restart nfs-utils and httpd, a daemon reload before it restarts the service.
Now lets try to install httpd and nfs-utils on web client
[root@web1 ~]# yum list nfs-utils httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: sg.fedora.ipserverone.com
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Available Packages
httpd.x86_64 2.4.6-89.el7.centos updates
nfs-utils.x86_64 1:1.3.0-0.61.el7 base
[root@web1 ~]#
[root@ansimaster playbooks]# cat multipalepacke.yml
# Simple anisble playbook to install multiple packages.
-
name: Play to install multiple pacakges
hosts: webservers
tasks:
- name: Ensure list of package present on web client
yum:
name: "{{Packages}}"
vars:
Packages:
- nfs-utils
- httpd
- name: start services for nfs and httpd
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
with_items:
- 'httpd'
- 'nfs-utils'
[root@ansimaster playbooks]#
[root@ansimaster playbooks]#
[root@ansimaster playbooks]# ansible-playbook multipalepacke.yml --syntax-check
playbook: multipalepacke.yml
[root@ansimaster playbooks]#
[root@ansimaster playbooks]# ansible-playbook multipalepacke.yml
PLAY [Play to install multiple pacakges] ***********************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.159.142]
TASK [Ensure list of package present on web client] ************************************************************************************
changed: [192.168.159.142]
TASK [start services for nfs and httpd] ************************************************************************************************
changed: [192.168.159.142] => (item=httpd)
changed: [192.168.159.142] => (item=nfs-utils)
PLAY RECAP *****************************************************************************************************************************
192.168.159.142 : ok=3 changed=2 unreachable=0 failed=0
==================================================================
Now go to web client and check for packages installed or not
==================================================================
[root@web1 ~]# yum list nfs-utils httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: sg.fedora.ipserverone.com
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Installed Packages
httpd.x86_64 2.4.6-89.el7.centos @updates
nfs-utils.x86_64 1:1.3.0-0.61.el7 @base
[root@web1 ~]#
============================================================================================================
Cheears packages has been installed on client now lets quickly check for services too......
============================================================================================================
[root@web1 ~]# systemctl status httpd; systemctl status nfs-utils;
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-05-30 11:15:04 EDT; 2min 55s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 15245 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─15245 /usr/sbin/httpd -DFOREGROUND
├─15247 /usr/sbin/httpd -DFOREGROUND
├─15248 /usr/sbin/httpd -DFOREGROUND
├─15249 /usr/sbin/httpd -DFOREGROUND
├─15250 /usr/sbin/httpd -DFOREGROUND
└─15251 /usr/sbin/httpd -DFOREGROUND
May 30 11:15:04 web1 systemd[1]: Starting The Apache HTTP Server...
May 30 11:15:04 web1 systemd[1]: Started The Apache HTTP Server.
May 30 11:15:04 web1 httpd[15245]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, usi... message
Hint: Some lines were ellipsized, use -l to show in full.
● nfs-utils.service - NFS server and client services
Loaded: loaded (/usr/lib/systemd/system/nfs-utils.service; static; vendor preset: disabled)
Active: active (exited) since Thu 2019-05-30 11:15:05 EDT; 2min 54s ago
Process: 15347 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 15347 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-utils.service
May 30 11:15:04 web1 systemd[1]: Stopping NFS server and client services...
May 30 11:15:04 web1 systemd[1]: Starting NFS server and client services...
May 30 11:15:05 web1 systemd[1]: Started NFS server and client services.
[root@web1 ~]#
=============================================================================
waooo services has been started by its own by ansible master server
=============================================================================
Starting multiple services using with_items:
You can use the with_items parameter to control multiple services in a single task.
Note: Make sure the {{ item }} is within quotes, and the with_items block need to be intended at the level of module name in our case its -here systemd. The following example will restart nfs-utils and httpd, a daemon reload before it restarts the service.
Now lets try to install httpd and nfs-utils on web client
[root@web1 ~]# yum list nfs-utils httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: sg.fedora.ipserverone.com
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Available Packages
httpd.x86_64 2.4.6-89.el7.centos updates
nfs-utils.x86_64 1:1.3.0-0.61.el7 base
[root@web1 ~]#
[root@ansimaster playbooks]# cat multipalepacke.yml
# Simple anisble playbook to install multiple packages.
-
name: Play to install multiple pacakges
hosts: webservers
tasks:
- name: Ensure list of package present on web client
yum:
name: "{{Packages}}"
vars:
Packages:
- nfs-utils
- httpd
- name: start services for nfs and httpd
systemd:
name: "{{ item }}"
state: restarted
daemon_reload: yes
with_items:
- 'httpd'
- 'nfs-utils'
[root@ansimaster playbooks]#
[root@ansimaster playbooks]#
[root@ansimaster playbooks]# ansible-playbook multipalepacke.yml --syntax-check
playbook: multipalepacke.yml
[root@ansimaster playbooks]#
[root@ansimaster playbooks]# ansible-playbook multipalepacke.yml
PLAY [Play to install multiple pacakges] ***********************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.159.142]
TASK [Ensure list of package present on web client] ************************************************************************************
changed: [192.168.159.142]
TASK [start services for nfs and httpd] ************************************************************************************************
changed: [192.168.159.142] => (item=httpd)
changed: [192.168.159.142] => (item=nfs-utils)
PLAY RECAP *****************************************************************************************************************************
192.168.159.142 : ok=3 changed=2 unreachable=0 failed=0
==================================================================
Now go to web client and check for packages installed or not
==================================================================
[root@web1 ~]# yum list nfs-utils httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.nbrc.ac.in
* epel: sg.fedora.ipserverone.com
* extras: mirror.nbrc.ac.in
* updates: mirror.nbrc.ac.in
Installed Packages
httpd.x86_64 2.4.6-89.el7.centos @updates
nfs-utils.x86_64 1:1.3.0-0.61.el7 @base
[root@web1 ~]#
============================================================================================================
Cheears packages has been installed on client now lets quickly check for services too......
============================================================================================================
[root@web1 ~]# systemctl status httpd; systemctl status nfs-utils;
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-05-30 11:15:04 EDT; 2min 55s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 15245 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─15245 /usr/sbin/httpd -DFOREGROUND
├─15247 /usr/sbin/httpd -DFOREGROUND
├─15248 /usr/sbin/httpd -DFOREGROUND
├─15249 /usr/sbin/httpd -DFOREGROUND
├─15250 /usr/sbin/httpd -DFOREGROUND
└─15251 /usr/sbin/httpd -DFOREGROUND
May 30 11:15:04 web1 systemd[1]: Starting The Apache HTTP Server...
May 30 11:15:04 web1 systemd[1]: Started The Apache HTTP Server.
May 30 11:15:04 web1 httpd[15245]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, usi... message
Hint: Some lines were ellipsized, use -l to show in full.
● nfs-utils.service - NFS server and client services
Loaded: loaded (/usr/lib/systemd/system/nfs-utils.service; static; vendor preset: disabled)
Active: active (exited) since Thu 2019-05-30 11:15:05 EDT; 2min 54s ago
Process: 15347 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 15347 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-utils.service
May 30 11:15:04 web1 systemd[1]: Stopping NFS server and client services...
May 30 11:15:04 web1 systemd[1]: Starting NFS server and client services...
May 30 11:15:05 web1 systemd[1]: Started NFS server and client services.
[root@web1 ~]#
=============================================================================
waooo services has been started by its own by ansible master server
=============================================================================
No comments:
Post a Comment