Restart apache if flag is true and there is change in a file

Viewed 67

So I want to restart Apache if the restart_httpd flag is true and if a new certificate is copied.

I don't know how to get Ansible to restart Apache if the flag is set to true and if the file has been changed. This is what I came up with so far, but is not working

  - name: Copy cert
    copy:
      src: "files/{{ cert_name }}"
      dest: "{{ cert_path }}/{{ item }}/"
      owner: root
      group: root
      mode: 0644
      backup: yes
    loop: "{{ list_cert_names }}"
    register: cert_copied

  - name: Restart Apache if variable is true
    debug:
      msg: 'Restart Apache Flag: {{ restart_httpd }}'
    when: restart_httpd and cert_copied.changed
    notify: Restart httpd
1 Answers

Test the flag restart_httpd in the handler

  handlers:
    - name: Restart httpd
      debug:
        msg: Restart httpd
      when: restart_httpd|bool

Given the certificates below, let's assume none of them has been installed yet

shell> tree files
files
├── cert1.pem
├── cert2.pem
└── cert3.pem

The task will copy the certificates and register the changes

    - copy:
        src: "files/{{ item }}"
        dest: "{{ cert_path }}/{{ item }}"
      loop: "{{ list_cert_names }}"
      register: cert_copied
      notify: Restart httpd

    - debug:
        var: cert_copied.results|map(attribute='changed')|list
    - debug:
        var: cert_copied.changed

gives

cert_copied.results|map(attribute='changed')|list:
  - true
  - true
  - true
cert_copied.changed: true

Because the task was changed the handler will be notified. If the flag is true the handler will execute the task

RUNNING HANDLER [Restart httpd] ****************************************
ok: [localhost] => 
  msg: Restart httpd

There is no need to explicitly test the changes. For example, the task won't be changed if no file is copied

TASK [copy] ************************************************************
ok: [localhost] => (item=cert1.pem)
ok: [localhost] => (item=cert2.pem)
ok: [localhost] => (item=cert3.pem)


TASK [debug] ***********************************************************
ok: [localhost] => 
  cert_copied.results|map(attribute='changed')|list:
  - false
  - false
  - false

TASK [debug] ***********************************************************
ok: [localhost] => 
  cert_copied.changed: false

You can see that there were no changes and, as a result, the handler wasn't notified. If, for example, only one file is changed the handler will be notified

TASK [copy] ************************************************************
ok: [localhost] => (item=cert1.pem)
changed: [localhost] => (item=cert2.pem)
ok: [localhost] => (item=cert3.pem)

TASK [debug] ***********************************************************
ok: [localhost] => 
  cert_copied.results|map(attribute='changed')|list:
  - false
  - true
  - false

TASK [debug] ***********************************************************
ok: [localhost] => 
  cert_copied.changed: true

RUNNING HANDLER [Restart httpd] ****************************************
ok: [localhost] => 
  msg: Restart httpd
Related