run ansible playbook 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: run ansible yaml file

ansible-playbook playbook.yml

Example 3: ansible example playbook

---
- name: update web servers
  hosts: webservers
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

- name: update db servers
  hosts: databases
  remote_user: root

  tasks:
  - name: ensure postgresql is at the latest version
    yum:
      name: postgresql
      state: latest
  - name: ensure that postgresql is started
    service:
      name: postgresql
      state: started

Example 4: 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.."