Is it possible to append a large amount of text to a file using Ansible

+1 vote

An application creates a lot of definitions in /etc/services. To keep services file handy with all these definitions is good practice so that we can just pipe them into /etc/services like this:

cp /etc/services /etc/services.stock
cat /path/to/build/services >> /etc/services

It works, but it's not idempotent i.e. running these commands repeatedly will cause the services file to get appended with the info again.

As I work through our Ansible playbooks, I'm trying to figure out how to do this. I could so something like this:

- command: "cat /path/to/build/services >> /etc/services"

but I don't want it to run every time I run the playbook.

Another option is to do something like this:

- name: add services
  lineinfile: 
    state: present
    insertafter: EOF
    dest: /etc/services
    line: "{{ item }}"
  with_items:
   - line 1
   - line 2
   - line 3
   - line 4
   - ...

It's not that fast, as it does each line individually.

Is there any other way around? Templates are not of that much help.

can anyone help me with this?

Thanks.

Jul 30, 2018 in Ansible by Damon Salvatore
• 5,980 points
14,673 views

1 answer to this question.

0 votes

Here is concept which you can follow.

blockinfile is a native, idempotent module to ensure a specified set of lines is present (absent) in a file.

Example:

- name: add services
  blockinfile: 
    state: present
    insertafter: EOF
    dest: /etc/services
    marker: "<!-- add services ANSIBLE MANAGED BLOCK -->"
    content: |
      line 1
      line 2
      line 3

I hope the above information will be helpful for you.

answered Jul 30, 2018 by Atul
• 10,240 points

Related Questions In Ansible

0 votes
1 answer

How to overwrite the content of a file in remote systems using Ansible playbook?

Hi@akhtar, You can find one argument in the ...READ MORE

answered Aug 2, 2020 in Ansible by MD
• 95,460 points
14,401 views
0 votes
1 answer

Is it possible to start ec2 Windows machine and run windows command using ansible playbook?

Hi@Lakshminarayanan, Yes, you can do that. But after ...READ MORE

answered Aug 14, 2020 in Ansible by MD
• 95,460 points
2,679 views
+1 vote
2 answers

Running ansible command on a single server when it is deployed to multiple servers

You can try the run_once attribute: Example from ...READ MORE

answered Jun 14, 2018 in Ansible by DareDev
• 6,890 points
2,464 views
0 votes
1 answer

Is it possible to run commands on ansible host?

If you just trying to run a ...READ MORE

answered Jan 10, 2019 in Ansible by Vijay
1,420 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
+2 votes
1 answer
0 votes
1 answer

How to use Ansible git module pull a branch with local changes?

You cannot achieve it using the git ...READ MORE

answered Jul 9, 2018 in Ansible by Atul
• 10,240 points
7,055 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP