How do I fill a property, whether unique or not, in a file using lineinfile?

Hello everyone.

I need to fill the shared_preload_libraries property of the postgresql.conf file with the value pg_stat_statements. It can be empty or not.

Examples:

Case 1)

Initial state:

#shared_preload_libraries = ‘’ # (change requires restart)

Final result:

shared_preload_libraries = ‘pg_stat_statements’ # (change requires restart)

Case 2)

Initial state:

shared_preload_libraries = ‘xxx,xxx2…,’ # (change requires restart)

Final result:

shared_preload_libraries = ‘xxx,xxx2…,pg_stat_statements’ # (change requires restart)

What have you tried? Please post your problem code/results…

Seems like this could be done with the ansible.builtin.replace module…

Try this:

---
- hosts: pg_servers

  vars:
    path: postgresql.conf
    splibs_regex: "^#?(shared_preload_libraries\\s+=\\s+')([^']*)('.*$)"
    extra_splibs:
      - pg_stat_statements

  tasks:
    - name: read file
      ansible.builtin.slurp:
        src: "{{ path }}"
      register: pgconf

    - ansible.builtin.set_fact:
        matches: "{{ pgconf.content | b64decode | regex_search(splibs_regex, '\\1', '\\2', '\\3', multiline=True) }}"

    - ansible.builtin.lineinfile:
        path: "{{ path }}"
        regexp: "{{ splibs_regex }}"
        line: "{{ matches[0] ~ matches[1] | split(',') | reject('equalto', '') | union(extra_splibs) | join(',') ~ matches[2] }}"
      when: matches

Hi dnmvisser,

I considered ‘localhost’ instead of ‘pg_servers’.

I got the error below:

"

TASK [ansible.builtin.lineinfile] ****************************************************************************************************************
fatal: [localhost]: FAILED! => {“msg”: “template error while templating string: no filter named ‘split’. String: {{ matches[0] ~ matches[1] | split(‘,’) | reject(‘equalto’, ‘’) | union(extra_splibs) | join(‘,’) ~ matches[2] }}”}
"

try ansible.builtin.split

Sorry, The error persists:

TASK [ansible.builtin.lineinfile] ****************************************************************************************************************
fatal: [localhost]: FAILED! => {“msg”: “template error while templating string: no filter named ‘ansible.builtin.split’. String: {{ matches[0] ~ matches[1] | ansible.builtin.split(‘,’) | reject(‘equalto’, ‘’) | union(extra_splibs) | join(‘,’) ~ matches[2] }}”}

what is the output of ‘ansible --version’ ?

1 Like

I think maybe you are on an outdated version of Ansible. This:

Hi,

Version: ansible 2.9.27

That is EOL, use a supported version

Thanks for your help, but right now that’s not possible. I work in a company where upgrades take a long time.
Is there another way to do it?


|

  • | - |

Thanks for your help, but right now that’s not possible. I work in a company where upgrades take a long time.
Is there another way to do it?

Move to a company where upgrades are not such a problem :slight_smile:
Or try one of the alternate installation methods, such as running it from a dedicated python venv.

That version of Ansible doesn’t have the split filter:

ansible.builtin.split filter – split a string into a list

New in ansible-core 2.11
ansible.builtin.split filter – split a string into a list — Ansible Community Documentation

Perhaps use the shell module and one of these suggestions?

Hi,

I really appreciate everyone’s help.
I’ll try to adapt it to what I need.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.