Part 1. Ansible palybook demo updating php.ini (memory_limit to 512M) to multiple hosts.
The goal is to make sure we are targeting the correct running/active php version using this command:
ps -aux | grep /etc/php/ | grep -v grep | awk '{print $14}' | awk -F'/' '{print $4}'
Playbook script:
- hosts: all
vars:
ansible_host_key_checking: false
# Workaround if LXC target host does not have python 3 (not by default)
ansible_python_interpreter: "/usr/bin/python3"
# vars_files:
# - vars.yml
become: true
become_user: root
# Ansible tasks stars here..
tasks:
# -- START Install Required repo and updates.
# Required for PHP FPM
- name: Getting the php version
shell: "ps -aux | grep /etc/php/ | grep -v grep | awk '{print $14}' | awk -F'/' '{print $4}'"
register: phpversion
- name: Updating php.ini memory_limi
ini_file:
path: /etc/php/{{ phpversion.stdout }}/fpm/php.ini
section: PHP
option: memory_limit
value: 512M
- name: Restart PHP
shell: "systemctl restart php{{ phpversion.stdout }}-fpm"
Demo screenshot:

Part 2. Enabling NGINX caching (fastcgi) using Ansible (4 different hosts/servers)
- hosts: all
become: true
become_user: root
# Ansible tasks starts here..
tasks:
- name: Insert fastcgi caching config in /etc/nginx/sites-available/default
blockinfile:
path: /etc/nginx/sites-available/default
# state: absent
marker: "### {mark} - Part 1 - Insert fastcgi module config - ANSIBLE MANAGED BLOCK ###"
insertbefore: '^server {'
block: |
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MY_CACHE:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
- name: Part 2 Insert fastcgi caching config in /etc/nginx/sites-available/default
blockinfile:
path: /etc/nginx/sites-available/default
# state: absent
marker: " ### {mark} - Part2 - Insert fastcgi module config - ANSIBLE MANAGED BLOCK ###"
insertafter: "fastcgi_pass unix:/run/php/php7.4-fpm.sock;"
block: |
include fastcgi_params;
fastcgi_cache MY_CACHE;
fastcgi_cache_valid 200 60m;
add_header jmc-lxd-X-Cache $upstream_cache_status;
- name: Test and reload nginx
shell: 'nginx -t; systemctl reload nginx'
~
