Clear Your Production Servers Cache Using Ansible

Simple script to clear the production cache and set the read/write permissions.
after the cache is cleared, the permission are set so the www server can access them... note the order is important, cache is cleared first, then perms set on the folders...

you'll need to change the following items to suit your flavour of linux:

    path: "/srv/www/htdocs/var/cache"
    owner: wwwrun
    group: www
and the targeted machines as named in your /etc/ansible/hosts file

  hosts: www

example hosts file:
[www]
192.168.0.1
192.168.0.2
192.168.0.3

  • name: Clear Applications Production Cache
    become: true
    hosts: www
    remote_user: root

    tasks:

    • name: Clear production cache
      command: php bin/console cache:clear --env=prod
      args:
      chdir: /srv/www/htdocs
      register: result
      changed_when: "'Cache cleared' in result.stdout or 'Clearing the cache' in result.stdout"

    • name: output log
      debug:
      var: result.stdout_lines

    • name: Clear production cache warmup
      command: php bin/console cache:warmup --env=prod
      args:
      chdir: /srv/www/htdocs
      register: result
      changed_when: "'Cache cleared' in result.stdout or 'Clearing the cache' in result.stdout"

    • name: output log
      debug:
      var: result.stdout_lines

    • name: Set ownership of cache folder to wwwrun:www
      file:
      path: "/srv/www/htdocs/var/cache"
      owner: wwwrun
      group: www
      recurse: yes
      state: directory

    • name: Set ownership of log folder to wwwrun:www
      file:
      path: "/srv/www/htdocs/var/log"
      owner: wwwrun
      group: www
      recurse: yes
      state: directory