1

I need to create an xml formated file in ansible. The file contains <> and spaces.

When I run the playbook without the <> or any white spaces between lines in it, the playbook creates the files.

How can I create a file with the following context

blockinfile:
  path: /tmp/testfile.txt
  content: |
          <example1>
            this is test1
            blah
            blah
          </example1>

         <example2>
          this is test2
          hello
         </example2>
1
  • Did Lewis' post answer your question? If so, mark the answer as "accepted" by klicking the check mark sign to the left of the answer!
    – mhutter
    Commented Sep 18, 2018 at 9:02

1 Answer 1

2

Look at the block section of blockinfile, https://docs.ansible.com/ansible/2.5/modules/blockinfile_module.html

Here is an example role

user1$ cat testblock/tasks/main.yml
---
- name: Testing blockinfile
  blockinfile:
    path: /tmp/testfile.txt
    block: |
      <example1>
         this is a test1
         blah
         blah
       </example1>

       <example2>
         this is test2
         hello
       </example2>
...

And here is the playbook:

user1$ cat testblock.yml
---
- hosts: localhost
  roles:
    - testblock
...

Running ansible-playbook ./testblock.yml produces the following file:

user1$ cat /tmp/testfile.txt
# BEGIN ANSIBLE MANAGED BLOCK
<example1>
   this is a test1
   blah
   blah
 </example1>

 <example2>
   this is test2
   hello
 </example2>
# END ANSIBLE MANAGED BLOCK

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .