Monday, January 22, 2018

Restart A service if new rpm installed.

 hosts: nodes
  tasks:
    name: Check package version
      shell: dpkg -s apache2 | grep -i version | awk -F ":" '{ print $2 }' | awk -F "-" '{ print $1 }'
      become: yes
      register: pkgversion
    debug: var=pkgversion
    name: Restart the service
      service: name=apache2 state=restarted
      become: yes
      when: '"2.4.18" in "{{ pkgversion.stdout }}"'



---
  - hosts: all
    tasks:
    - name: Check RPM version
      shell: /usr/bin/yum list httpd | grep httpd | awk '{print $2}' | cut -d'-' -f1
      register: installedver
    - name: Create NOT_UPDATED File
      local_action: lineinfile path=/home/test/books/check_rpm/NOT_UPDATED line="||HOSTNAME||INSTALLED_VERSION||" insertbefore=BOF state=present create=yes
    - name: Create UPDATED File
      local_action: lineinfile path=/home/test/books/check_rpm/UPDATED line="||HOSTNAME||INSTALLED_VERSION||PID_BR||PID_AR||RESTART_STAT||" insertbefore=BOF state=present create=yes
    - name: If installed version is as required take PID before restart
      shell: ps -ef | grep feedprocessor | grep -v grep | awk '{print $2}'
      register: pidbr
      when: installedver.stdout == '1.7.0_2'
    - name: Restart service after taking PID
      copy:
       content: "hello world\n"
       dest: /home/test/testfile
      notify: update_status
      when: installedver.stdout == '1.7.0_2'
    - name: If installed version is as required take PID before restart
      shell: ps -ef | grep httpd | grep -v grep | awk '{print $2}'
      register: pidar
    - name: Update to NOT_INSTALLED file when package is not latest
      local_action: lineinfile path=/home/test/books/check_rpm/NOT_UPDATED line="{{ ansible_fqdn }}|{{ installedver.stdout }}" insertafter=EOF state=present create=yes
      when: installedver.stdout != '1.7.0_2'
    handlers:
    - name: update_status
      local_action: lineinfile path=/home/test/books/check_rpm/UPDATED line="|{{ ansible_fqdn }}|{{ installedver.stdout }}|{{ pidbr.stdout }}|{{ pidar.stdout }}|restarted|" insertafter=EOF state=present

...






||HOSTNAME||INSTALLED_VERSION||PID_BR||PID_AR||RESTART_STAT||
|ansic1.example.com|2.4.6|5448|5721|restarted|
|ansic2.example.com|2.4.6|4640|4913|restarted|
|ansic1.example.com|2.4.6|5721|5988|restarted|
|ansic2.example.com|2.4.6|4913|5185|restarted|
[root@ansim0 ~]# cat rpm_check.yaml
---
  - hosts: http
    tasks:
    - name: Check RPM version
      shell: rpm -qa | grep httpd-2 | cut -d'-' -f2
      register: installedver
    - name: Create NOT_UPDATED File
      local_action: lineinfile dest=/root/NOT_UPDATED line="||HOSTNAME||INSTALLED_VERSION||" insertbefore=BOF state=present create=yes
    - name: Create UPDATED File
      local_action: lineinfile dest=/root/UPDATED line="||HOSTNAME||INSTALLED_VERSION||PID_BR||PID_AR||RESTART_STAT||" insertbefore=BOF state=present create=yes
    - name: If installed version is as required take PID before restart
      shell: ps -ef | grep httpd | grep -v grep | head -n 1 |awk '{print $2}'
      register: pidbr
      when: installedver.stdout == '2.4.6'
    - name: Restart service after taking PID
      service:
       name: httpd
       state: restarted
      notify: update_status
      when: installedver.stdout == '2.4.6'
    - name: If installed version is as required take PID before restart
      shell: ps -ef | grep httpd | grep -v grep | head -n 1 |awk '{print $2}'
      register: pidar
    - name: Update to NOT_INSTALLED file when package is not latest
      local_action: lineinfile dest=/root/NOT_UPDATED line="{{ ansible_fqdn }}|{{ installedver.stdout }}" insertafter=EOF state=present create=yes
      when: installedver.stdout != '2.4.6'
    handlers:
    - name: update_status
      local_action: lineinfile dest=/root/UPDATED line="|{{ ansible_fqdn }}|{{ installedver.stdout }}|{{ pidbr.stdout }}|{{ pidar.stdout }}|restarted|" insertafter=EOF state=present




...

Monday, January 8, 2018

Ansible Dry Run

Ansible has a option to run it in dry mode. (i.e) Without executing it will show what it is going to execute or change.


# ansible-playbook myplaybook.yaml --check