regular expression – Ansible: quote square brackets in variable value for regexp parameter

For the modification of a PHP-FPM pool configuration file I use ansible.builtin.lineinfile. How should I quote the square brackets in a value of a variable for the regexp parameter?

Without quoting the square brackets are interpreted as bracket expression – a list of characters to be matching. Without Ansible I would use sed to add a backslash.

The following example is for a Debian default pool conf file.

- name: Change PHP-FPM pool parameters
  become: true
    path: "/etc/php/{{ php_fpm_version }}/fpm/pool.d/www.conf"
    regexp: "^;?{{ item['para'] }} ="
    line: '{{ item.para }} = {{ item.value }}'
  loop: "{{ php_fpm_pool_parameters }}"
    - para: 'pm.max_spare_servers'
      value: '7'
    - para: 'pm.max_requests'
      value: '500'
    - para: 'env[PATH]'
      value: '/usr/bin:/bin'
    - para: 'php_admin_value[memory_limit]'
      value: '4096M'
    - para: 'php_admin_value[max_input_time]'
      value: '600'

Read more here: Source link