Adding Multiple lines to file

560 views
Skip to first unread message

Mark Matthews

unread,
Feb 10, 2016, 12:11:21 PM2/10/16
to Ansible Project
Hi

I want to add multiple lines to a host file on some Windows machines.

I tried using win_lineinfile in the following playbook below...but it only added the last line.

---
- name: Add host file entries
  hosts: all
  tasks:
    - name: Check if line is present in config
      win_lineinfile:
        dest: C:\Windows\System32\drivers\etc\hosts
        line: "x.x.x.x   www.website1.com"
        line: "x.x.x.x   www.website2.com"
        line: "x.x.x.x   www.website3.com"
        line: "x.x.x.x   www.website4.com"


Is there a way of doing this?

Matt Martz

unread,
Feb 10, 2016, 12:15:56 PM2/10/16
to ansible...@googlegroups.com
You would probably want to loop over a list of "lines" using with_items, and them just use "{{ item }}" for the "line" argument.

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/369620ad-7b20-44a6-80ba-c70f3573798f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Mark Matthews

unread,
Feb 11, 2016, 5:19:56 AM2/11/16
to Ansible Project
Hi Matt

How exactly would that look like in a playbook?

Cheers
Mark

J Hawkesworth

unread,
Feb 11, 2016, 6:31:47 AM2/11/16
to Ansible Project
Lots of examples of the various ways to loop here: http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops

Depending on what you are trying to achieve, you might actually find the win_template module is easier to work with rather than multiple calls to win_lineinfile

Hope this helps,

Jon

Mark Matthews

unread,
Feb 11, 2016, 12:04:22 PM2/11/16
to Ansible Project
Hi Jon

Thanks for the response.
I was actually able to find a work around. Not a great solution, so will look at win_template.

--- 
- name: Add host file entries 
hosts: all 
tasks: 

  - name: Add host file entries 


    win_lineinfile: 
       dest: C:\Windows\System32\drivers\etc\hosts 
       line: | 

         x.x.x.x www.wesite1.com\r\n 
         x.x.x.x www.wesite2.com\r\n 
         x.x.x.x www.wesite3.com\r\n 
         x.x.x.x www.wesite4.com\r\n 
         x.x.x.x www.wesite5.com



Cheers
Mark


On Wednesday, February 10, 2016 at 5:11:21 PM UTC, Mark Matthews wrote:
Reply all
Reply to author
Forward
0 new messages