ansible playbook to add multiple lines to conf file

471 views
Skip to first unread message

Tony Wong

unread,
Jan 31, 2023, 3:41:47 PM1/31/23
to Ansible Project
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 iburst"
- "server ntp2.domain.com iburst"
- "server ntp3.domain.com iburst"

Rowe, Walter P. (Fed)

unread,
Jan 31, 2023, 3:49:33 PM1/31/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/836036dd-6862-4475-b509-786762a2a7cbn%40googlegroups.com.

Tony Wong

unread,
Jan 31, 2023, 3:56:20 PM1/31/23
to ansible...@googlegroups.com
the source is in my vars.yml file

line:
  line_to_add:
    - "server ntp1.domain.com iburst"
    - "server ntp2.domain.com  iburst"
    - "server ntp3.domain.com iburst"


variable is line_to_add


You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Hm2pRcVckcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/C1985159-C94B-4703-9CB7-9F032DD182FE%40nist.gov.

Richard Megginson

unread,
Jan 31, 2023, 3:56:23 PM1/31/23
to ansible...@googlegroups.com

Tony Wong

unread,
Jan 31, 2023, 4:08:06 PM1/31/23
to ansible...@googlegroups.com
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

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Hm2pRcVckcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALF5A-%2BFZhAfS9Hvo%3DAtqf3T%2BfPA6mpTy8QpaZH%3Dsmd3-_5kTA%40mail.gmail.com.

Rowe, Walter P. (Fed)

unread,
Jan 31, 2023, 4:13:10 PM1/31/23
to ansible...@googlegroups.com
This is a LIST all by itself. Its reference is line.line_to_add.

  line_to_add:
    - "server ntp1.domain.com iburst"
    - "server ntp2.domain.com  iburst"
    - "server ntp3.domain.com iburst"

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

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Tony Wong

unread,
Jan 31, 2023, 4:18:09 PM1/31/23
to ansible...@googlegroups.com
its this

line:
line_to_replace: "server ntp1a.domain.com iburst"
replace_with: "#server ntp1a.domain.com iburst"
line_to_add:
- "server ntp1.domain.com iburst"
- "server ntp2.domain.com iburst"
- "server ntp3.domain.com iburst"


Tony Wong

unread,
Jan 31, 2023, 4:27:10 PM1/31/23
to ansible...@googlegroups.com
if it was only 1 line then its fine

line_to_add: "server ntp1.domain.com iburst"

but not multiple lines
Reply all
Reply to author
Forward
0 new messages