I use the ansible.builtin.lineinfile
module to modify a PHP-FPM pool configuration file. How should I quote the square brackets (annot.: or other special characters) 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
ansible.builtin.lineinfile:
path: "/etc/php/{{ php_fpm_version }}/fpm/pool.d/www.conf"
regexp: "^;?{{ item.para }} ="
line: '{{ item.para }} = {{ item.value }}'
loop: "{{ php_fpm_pool_parameters }}"
vars:
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'
line: '{{ item.para }}'
you can also write inregexp: "^;?{{ item.para }} ="
. There should be no need for an other syntax. Documentation: Referencing nested variables.{{ item['para'] }}
was an attempt to solve the problem by avoiding conflicts like described here: Referencing key:value dictionary variables The problem is that regex is supposed to search for [PATH], including the square bracket, and not P, A, T, or H.