我需要删除文件主机中的所有字符串,并在字符串“127.0.0.1 localhost”后添加一行。
我曾尝试过使用模块替换,但我不明白我需要放在“之后”:
你会好起来的 模板
例如模板hosts.j2
127.0.0.1 localhost {% for item in my_hosts %} {{ item }} {% endfor %}
与剧本
- hosts: localhost vars: my_hosts: - 10.1.0.1 ac1.example.com ac1 - 10.1.0.2 ac2.example.com ac2 - 10.1.0.3 ac3.example.com ac3 tasks: - template: src: hosts.j2 dest: /etc/hosts
给
> cat /etc/hosts 127.0.0.1 localhost 10.1.0.1 ac1.example.com ac1 10.1.0.2 ac2.example.com ac2 10.1.0.3 ac3.example.com ac3