ansible playbook to add multiple lines to conf file

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:

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

There’s also https://github.com/linux-system-roles/timesync

the source is in my vars.yml file

line:
line_to_add:

variable is line_to_add

here is my output

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

This is a LIST all by itself. Its reference is line.line_to_add.

line_to_add:

You have no value set for “replace_with”. What sets that?

Walter

its this

line:
line_to_replace: “server ntp1a.domain.com iburst”
replace_with: “#server ntp1a.domain.com iburst”
line_to_add:

if it was only 1 line then its fine

line_to_add: “server ntp1.domain.com iburst”

but not multiple lines