ansible block of code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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>









share|improve this question





















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














up vote
0
down vote

favorite












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>









share|improve this question





















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












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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>









share|improve this question













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>






ansible






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 10 at 17:25









user2236794

262




262











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
















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















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 at 9:02




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 at 9:02










1 Answer
1






active

oldest

votes

















up vote
2
down vote













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


Hope this helps.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468061%2fansible-block-of-code%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    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


    Hope this helps.






    share|improve this answer


























      up vote
      2
      down vote













      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


      Hope this helps.






      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        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


        Hope this helps.






        share|improve this answer














        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


        Hope this helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 10 at 17:48









        Jeff Schaller

        33.1k849111




        33.1k849111










        answered Sep 10 at 17:40









        Lewis M

        3813




        3813



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468061%2fansible-block-of-code%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)