Appending a line in a file

40 views
Skip to first unread message

lpesc...@google.com

unread,
Feb 25, 2019, 8:30:28 PM2/25/19
to Ansible Project
Hi, 
I want to add some stuff to my /etc/hosts file. 
For instance, if my /etc/hosts says "server.corp", I want to append that in all my machines to where it says "server.corp.company.com". Then I want it to save.
What is the best module for that , and can you tell me what the play might look like?

Luca Cazzaniga

unread,
Feb 25, 2019, 8:47:00 PM2/25/19
to ansible...@googlegroups.com
Hi try the module lineinfile, see the manual page at https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html

--
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/cb9896bd-1639-4690-b8d7-457c5ab75617%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lpesc...@google.com

unread,
Feb 25, 2019, 9:02:05 PM2/25/19
to ansible...@googlegroups.com
Thanks Luca, I sorta thought that was it, could you give a quick example on how the file would look?

Sent from my iPhone. Please excuse any typos.

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/mYI1oZ8hAyA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Kylix Kumar

unread,
Feb 25, 2019, 9:10:33 PM2/25/19
to ansible...@googlegroups.com

lpesc...@google.com

unread,
Feb 25, 2019, 9:12:08 PM2/25/19
to ansible...@googlegroups.com
Thanks kylix,
Where would the append go?


Sent from my iPhone. Please excuse any typos.

fusillator

unread,
Feb 25, 2019, 9:41:37 PM2/25/19
to Ansible Project

I don't  know if I got your specification correctly

anyway this is an example


$ cat lineinfile.yml
- hosts: localhost
  tasks:

  - name: lineinfile
    lineinfile:
      path: ./hosts
      regexp: '^(.*) server\.corp\b(.*)$'
      line: '\1  server.corp server.corp.company.com \2'
      backrefs: yes

$ cat hosts
127.0.0.1    localhost
192.168.192.168  server.corp
fusillator@catorcio:~/Code/ansible/test$ ansible-playbook lineinfile.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [lineinfile] *************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0  

$ cat hosts
127.0.0.1    localhost
192.168.192.168   server.corp server.corp.company.com


regards


Luca

Larry Pescatore

unread,
Feb 25, 2019, 9:56:54 PM2/25/19
to ansible...@googlegroups.com
Thank you sir, I am trying to find out how that exactly would look in my file (I am new to regexp so please forgive me). 
Heres what my file looks like:

 cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 server-rack5-test-server-5

What I need it to change to (on a dynamic level, as in "server-rack5-test-server-5" will of course always be different"):
 cat /etc/hosts
127.0.0.1 localhost

Could you help me with that?


For more options, visit https://groups.google.com/d/optout.


--
Best, 
Larry Pescatore
Lab Administrator @ Pixel 


fusillator

unread,
Feb 26, 2019, 3:51:35 AM2/26/19
to Ansible Project
hi, you have to find common pattern in the hostnames and forge subregexp in brakets..
for example if the changing part is the number enclose them in brackets and backreference them in line as \cardianalnumber
in the example below
\1 will reference to (.*)
\2 will reference to the first (\d+)
\3 will reference to the second (\d+)
\4 will reference to the tailing (.*)

At the moment I can't test the playbook, anyway you try something like

- hosts: localhost
  tasks:

  - name: lineinfile
    lineinfile:
      path: ./hosts
      regexp: '^(.*)\bserver-rack(\d+)-test-server-(\d+)\b(.*)$'
      line: '\1  server-rack\2-test-server-\3.corp.company.com \4'
      backrefs: yes

Have a look at https://docs.python.org/2/library/re.html for further details

regards

Luca

fusillator

unread,
Mar 4, 2019, 10:07:52 AM3/4/19
to ansible...@googlegroups.com

I don't  know if I got your specification correctly

anyway this is an example

fusillator@catorcio:~/Code/ansible/test$ cat lineinfile.yml

- hosts: localhost
  tasks:

  - name: lineinfile
    lineinfile:
      path: ./hosts
      regexp: '^(.*) server\.corp\b(.*)$'
      line: '\1  server.corp server.corp.company.com \2'
      backrefs: yes

fusillator@catorcio:~/Code/ansible/test$ cat hosts

127.0.0.1    localhost
192.168.192.168  server.corp
fusillator@catorcio:~/Code/ansible/test$ ansible-playbook lineinfile.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [lineinfile] *************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0  

fusillator@catorcio:~/Code/ansible/test$ cat hosts

127.0.0.1    localhost
192.168.192.168   server.corp server.corp.company.com

regards


Luca

Reply all
Reply to author
Forward
0 new messages