0

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>
  • 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 Sep 18 '18 at 9:02
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.