# ansible playbook to add multiple lines to conf file

**URL:** <https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598>\
**Category:** Ansible Project\
**Created:** [January 31, 2023, 8:41pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598 "2023-01-31T20:41:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 31, 2023, 8:41pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/1 "2023-01-31T20:41:47Z")

</div>

trying to add multiple ntp servers to chrony.conf

- name: add line  
lineinfile:  
backup: no  
backrefs: no  
state: present  
path: “{{ file\_path }}”  
insertafter: “{{ line.replace\_with }}”  
line: “{{ line.line\_to\_add }}”  
with\_items:
- { search: “{{ line.replace\_with }}”, add: “{{ line.line\_to\_add }}” }

but it only adds it as single line

any idea?

vars

line:

line\_to\_add:

- “server [ntp1.domain.com](http://ntp1.domain.com) iburst”
- “server [ntp2.domain.com](http://ntp2.domain.com) iburst”
- “server [ntp3.domain.com](http://ntp3.domain.com) iburst”

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 31, 2023, 8:49pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/2 "2023-01-31T20:49:15Z")

</div>

Where are you sourcing the “lines” you want to add to the file? What variable contains that list? You reference “line”. What is the format/type of that variable?

- name: add line  
lineinfile:  
backup: no  
backrefs: no  
state: present  
path: “{{ file\_path }}”  
insertafter: “{{ item.search }}”  
line: “{{ item.add }}”  
with\_items:
- { search: “{{ line.replace\_with }}”, add: “{{ line.line\_to\_add }}” }

Walter

---

<div class="post-metadata">

**Author:** ![Richard\_Megginson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/richard_megginson/32/4540_2.png) [@Richard\_Megginson](https://forum.ansible.com/u/Richard_Megginson)\
**Post date:** [January 31, 2023, 8:55pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/3 "2023-01-31T20:55:54Z")

</div>

There’s also [https://github.com/linux-system-roles/timesync](https://github.com/linux-system-roles/timesync)

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 31, 2023, 8:55pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/4 "2023-01-31T20:55:54Z")

</div>

the source is in my vars.yml file

line:  
line\_to\_add:

- “server [ntp1.domain.com](http://ntp1.domain.com) iburst”
- “server [ntp2.domain.com](http://ntp2.domain.com) iburst”
- “server [ntp3.domain.com](http://ntp3.domain.com) iburst”

variable is line\_to\_add

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 31, 2023, 9:07pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/5 "2023-01-31T21:07:42Z")

</div>

here is my output

[WARNING]: The value “[‘server [ntp1.domain.com](http://ntp1.domain.com) iburst’, ‘server [ntp2.domain.com](http://ntp2.domain.com) iburst’, ‘server [ntp3.domain.com](http://ntp3.domain.com) iburst’]” (type list) was  
converted to “u”[‘server [ntp1.domain.com](http://ntp1.domain.com) iburst’, ‘server [ntp2.domain.com](http://ntp2.domain.com) iburst’, ‘server [ntp3.domain.com](http://ntp3.domain.com) iburst’]“” (type string). If this  
does not look like what you expect, quote the entire value to ensure it does no

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 31, 2023, 9:12pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/6 "2023-01-31T21:12:52Z")

</div>

This is a LIST all by itself. Its reference is line.line\_to\_add.

> line\_to\_add:
> 
> - “server [ntp1.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp1.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=psp1aCOvDNbhf5mnUQsf6nN8ptGhPqWgU6PyknyqTAY%3D&reserved=0) iburst”
> - “server [ntp2.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp2.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=c8JeCg%2FXuIkY6Q%2BtWGGOi%2B8lSuqgvW7JGR0n%2Bah8zlI%3D&reserved=0) iburst”
> - “server [ntp3.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp3.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=oKIXGadK1OaM%2FVjT20xoEPhL2GBlwQdt1Oxiy5xlCU4%3D&reserved=0) iburst”

You have no value set for “replace\_with”. What sets that?

Walter

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 31, 2023, 9:17pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/7 "2023-01-31T21:17:46Z")

</div>

its this

line:  
line\_to\_replace: “server [ntp1a.domain.com](http://ntp1a.domain.com) iburst”  
replace\_with: “#server [ntp1a.domain.com](http://ntp1a.domain.com) iburst”  
line\_to\_add:

- “server [ntp1.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp1.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=psp1aCOvDNbhf5mnUQsf6nN8ptGhPqWgU6PyknyqTAY%3D&reserved=0) iburst”
- “server [ntp2.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp1.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=psp1aCOvDNbhf5mnUQsf6nN8ptGhPqWgU6PyknyqTAY%3D&reserved=0) iburst”
- “server [ntp3.domain.com](https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fntp1.domain.com%2F&data=05%7C01%7Cwalter.rowe%40nist.gov%7C1b86b7cf47d54642ab6208db03cd9597%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638107953735364130%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=psp1aCOvDNbhf5mnUQsf6nN8ptGhPqWgU6PyknyqTAY%3D&reserved=0) iburst”

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 31, 2023, 9:26pm UTC](https://forum.ansible.com/t/ansible-playbook-to-add-multiple-lines-to-conf-file/37598/8 "2023-01-31T21:26:44Z")

</div>

if it was only 1 line then its fine

line\_to\_add: “server [ntp1.domain.com](http://ntp1.domain.com) iburst”

but not multiple lines
