Ansible blockinfile模块生成文件时块首出现False的问题解决
解决Ansible blockinfile模块生成内容前出现"False"字符串的问题
这个问题我之前也碰到过,根源出在blockinfile模块的marker参数设置上!你写的marker: no在YAML语法里会被自动解析成布尔值False,而blockinfile模块在处理这个参数时,会把布尔值直接转换成字符串并插入到每个生成块的开头,这就导致了每行前面都多了个"False"。
修复方案
如果你完全不需要blockinfile生成的标记注释(也就是不想在文件里留下任何Ansible管理块的标记),只需要把marker参数设置为空字符串"",而不是布尔值no。
修改后的任务代码
- name: Test to generate config file hosts: localhost tasks: - name: Blockfile test blockinfile: path: blockfile_test create: yes marker: "" # 替换原来的no,设置为空字符串禁用标记 insertafter: EOF content: "{{ lookup('template', 'blockfile_template') }}" with_items: - One - Two - Three
验证效果
重新运行playbook后,生成的文件内容就会符合预期:
This is test to generate new file Count # One This is test to generate new file Count # Two This is test to generate new file Count # Three
补充说明
blockinfile模块的marker参数预期接收的是字符串类型(默认值是#{mark} ANSIBLE MANAGED BLOCK),当你传入布尔值时,模块内部会强制将其转换为字符串,于是"False"就被当成了标记前缀插入到内容中。设置为空字符串就能完全禁用标记生成,完美匹配你的需求。
内容的提问来源于stack exchange,提问作者V Mahaja
相关产品推荐
相关产品推荐

