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

**URL:** <https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [September 23, 2024, 12:33pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706 "2024-09-23T12:33:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 23, 2024, 12:33pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/1 "2024-09-23T12:33:48Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [September 24, 2024, 3:49pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/2 "2024-09-24T15:49:09Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 24, 2024, 4:06pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/3 "2024-09-24T16:06:41Z")

</div>

Try this:

```yaml
---
- 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

```

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 24, 2024, 5:55pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/4 "2024-09-24T17:55:38Z")

</div>

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] }}”}  
"

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 24, 2024, 6:23pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/5 "2024-09-24T18:23:10Z")

</div>

try ansible.builtin.split

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 24, 2024, 7:10pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/6 "2024-09-24T19:10:28Z")

</div>

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] }}”}

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 24, 2024, 7:38pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/7 "2024-09-24T19:38:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 24, 2024, 7:47pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/8 "2024-09-24T19:47:56Z")

</div>

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

> [@How do I fill a property, whether unique or not, in a file using lineinfile?](http://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/7):
>
> what is the output of ‘ansible --version’ ?

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 24, 2024, 10:24pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/9 "2024-09-24T22:24:31Z")

</div>

Hi,

Version: ansible 2.9.27

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 25, 2024, 2:15am UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/10 "2024-09-25T02:15:52Z")

</div>

That is EOL, use a supported version

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 25, 2024, 10:13am UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/11 "2024-09-25T10:13:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 25, 2024, 10:52am UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/12 "2024-09-25T10:52:25Z")

</div>

> [@How to convert this to ansible?](http://forum.ansible.com/t/how-to-convert-this-to-ansible/8618/2):
>
> This forum isn’t here for people to code for you, it is for us to help people who have issues with code. You should use the Ansible documentation and Google to figure out how to do each step you have listed, and build out a playbook and/or role piece by piece, troubleshooting as needed. If/when you have issues that you cannot seem to get by, then feel free to post here. Good luck!

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [September 25, 2024, 11:14am UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/13 "2024-09-25T11:14:48Z")

</div>

> > > |
> > 
> > - | - |
> 
> 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 🙂  
Or try one of the alternate installation methods, such as running it from a dedicated python venv.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 25, 2024, 11:33am UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/14 "2024-09-25T11:33:18Z")

</div>

> [@Luiz\_Gustavo](#):
>
> Version: ansible 2.9.27

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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/split_filter.html)

Perhaps use the `shell` module and [one of these suggestions](https://stackoverflow.com/a/15988793)?

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [September 25, 2024, 11:09pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/15 "2024-09-25T23:09:18Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 25, 2024, 11:10pm UTC](https://forum.ansible.com/t/how-do-i-fill-a-property-whether-unique-or-not-in-a-file-using-lineinfile/8706/16 "2024-10-25T23:10:17Z")

</div>

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