我需要在ansible中创建一个xml格式的文件。该文件包含 <> 和空格。
当我运行没有 <> 或行间任何空格的剧本时,剧本会创建文件。
如何创建具有以下上下文的文件
blockinfile:
path: /tmp/testfile.txt
content: |
<example1>
this is test1
blah
blah
</example1>
<example2>
this is test2
hello
</example2>
答案1
查看blockinfile的块部分,https://docs.ansible.com/ansible/2.5/modules/blockinfile_module.html
这是一个示例角色
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>
...
这是剧本:
user1$ cat testblock.yml
---
- hosts: localhost
roles:
- testblock
...
运行ansible-playbook ./testblock.yml
会产生以下文件:
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