Making httpd services non-idempotent
Task Description:
Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook
Whenever we run ansible playbook without changing conf settings or some changes it will automatically start httpd services because we have given key value as restarted so it consumes more resources although we haven’t done any changes
So here to make these services not idempotence I am gonna use when keyword
Here is my ansible playbook code
- hosts: all
vars:
- dvd_dir: "/dvd1"
tasks:
- file:
state: directory
path: "{{ dvd_dir }}"
- mount:
src: "/dev/cdrom"
path: "{{ dvd_dir }}"
state: mounted
fstype: iso9660
- yum_repository:
baseurl: "{{ dvd_dir }}/AppStream"
name: "dvd1"
gpgcheck: no
description: "Yum repo 1"
- yum_repository:
baseurl: "{{ dvd_dir }}/BaseOS"
name: "dvd2"
gpgcheck: no
description: "Yum repo 2"
- package:
name: "httpd"
state: present
- copy:
dest: /var/www/html/index.html
register: result
- copy:
src: /home/kanav/t.conf
dest: /etc/httpd/conf.d
- service:
name: "httpd"
state: restarted
when: result.changed
At first execution attempt it restarts services because we are uploading conf file for first time
After first execution of playbook if we run playbook without any changes it will not restart services