ansible playbook restart service code example

Example 1: ansible playbook enable service

- name: Start service httpd, if not started
  service:
    name: httpd
    state: started

- name: Stop service httpd, if started
  service:
    name: httpd
    state: stopped

- name: Restart service httpd, in all cases
  service:
    name: httpd
    state: restarted

- name: Reload service httpd, in all cases
  service:
    name: httpd
    state: reloaded

- name: Enable service httpd, and not touch the state
  service:
    name: httpd
    enabled: yes

- name: Start service foo, based on running process /usr/bin/foo
  service:
    name: foo
    pattern: /usr/bin/foo
    state: started

- name: Restart network service for interface eth0
  service:
    name: network
    state: restarted
    args: eth0

Example 2: ansible playbook post deployment

ps -aef|grep ClientProvision.exe
if it is running we can go & check in logs as well
cd /opt/Logs/dmApp
current hour application log we can grep & see printing in logs
date=date '+%d-%m-%y_%H_'
cat ClientProv_30xcf_$date *.log |grep "Loading InRoamer Details.."
if it is returing result as below we can ensure it is up & running fine
[InRoamer.cpp|00040|05:06:34:396|DG]Loading InRoamer Details
date=date '+%d_%m_%Y_05_'
#echo $date
cd /opt/Logs/dmApp
logfile=ClientProv_30xcf_$date*.log
#echo $logfile
cat $logfile |grep "Loading InRoamer Details.."