lineinfile
该模块是操作文件中的每一行内容,他是按照行为单位的,和下面的replace
模块并不冲突。
tasks:
- name: Ensure SELinux is set to enforcing mode
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=enforcing
把 SELINUX=
这个开头的行直接替换成SELINUX=enforcing
不管后面是什么,直接替换整行内容。
- name: 确保sudoers配置中没有wheel组。
lineinfile:
path: /etc/sudoers
state: absent
regexp: '^%wheel'
- name: Ensure the default Apache port is 8080
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen ' //确保添加完成
insertafter: '^#Listen ' //要在哪一行前面添加
line: Listen 8080 //添加的内容
- name: Ensure we have our own comment added to /etc/services
lineinfile:
path: /etc/services
regexp: '^# port for http' //确保添加完成
insertbefore: '^www.*80/tcp' //要在哪一行后面添加
line: '# port for http by default' //添加的内容是什么
- name: Replace a localhost entry with our own
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: 127.0.0.1 localhost
owner: root
group: root
mode: '0644'
- name: add a line
lineinfile:
dest: /etc/hosts
line: '10.1.1.1 zhangshoufu.com'
replace
模块可以根据我们指定的正则表达式替换匹配到的字符串,文件中所有被匹配到的字符串都会被替换,和lineinfile
不同的地方是replace
只会替换正则表达式匹配到的内容,而lineinfile
是替换正则表达式匹配到行的内容。
tasks:
- name: '替换zsf 字符串为zhangshoufu'
replace:
path: /tmp/test.txt
regexp: 'zsf'
replace: 'zhangshoufu'
注释 Apache 配置文件/etc/apache2/sites-available/default.conf
中NameVirtualHost [*]
行之后的所有内容:
- name: Replace after the expression till the end of the file (requires Ansible >= 2.4)
replace:
path: /etc/apache2/sites-available/default.conf
after: 'NameVirtualHost [*]'
regexp: '^(.+)$'
replace: '# \1'
注释 Apache 配置文件/etc/apache2/sites-available/default.conf
中# live site config
行之前的所有内容:
- name: Replace before the expression till the begin of the file (requires Ansible >= 2.4)
replace:
path: /etc/apache2/sites-available/default.conf
before: '# live site config'
regexp: '^(.+)$'
replace: '# \1'
注释 Apache 配置文件/etc/apache2/sites-available/default.conf
中<VirtualHost [*]>
行和</VirtualHost>
行之间的内容:
# Prior to Ansible 2.7.10, using before and after in combination did the opposite of what was intended.
# see https://github.com/ansible/ansible/issues/31354 for details.
- name: Replace between the expressions (requires Ansible >= 2.4)
replace:
path: /etc/apache2/sites-available/default.conf
after: '<VirtualHost [*]>'
before: '</VirtualHost>'
regexp: '^(.+)$'
replace: '# \1'
- name: Supports a validate command
replace:
path: /etc/apache/ports
regexp: '^(NameVirtualHost|Listen)\s+80\s*$'
replace: '\1 127.0.0.1:8080'
validate: '/usr/sbin/apache2ctl -f %s -t'
和lineinfile
有点类似,他可以帮助我们在文件中插入一段文本。
# BEGIN ANSIBLE MANAGED BLOCK
,结束标记为# END ANSIBLE MANAGED BLOCK
。
{mark}
变量会自动被替换成开始标记中的marker_begin
和结束标记中的marker_end
,如果使用没有{mark}
变量的自定义标记,可能会导致重复插入。
EOF
或者正则表达式,默认值为EOF
,表示End Of File
,插入到文件的末尾。
如果设置为正则表达式,默认将文本插入到正则表达式匹配的最后一行之后。
如果设置为正则表达式,但是没有匹配到任何行,则插入到文件末尾。
BOF
或者正则表达式,默认值为BOF
,表示Begin Of File
,插入到文件的开头。
如果设置为正则表达式,默认将文本插入到正则表达式匹配的最后一行之前。
如果设置为正则表达式,但是没有匹配到任何行,则插入到文件开头。
tasks:
- name: 'add hosts'
blockinfile:
path: /etc/hosts
block: |
10.1.1.1 www.zhangshoufu.com
10.2.2.2 test.zhangshoufu.com
备份/etc/ssh/ssh_config文件,并在文件末尾插入./local/ssh_config文件的内容,最后使用/usr/sbin/sshd -T -f /etc/ssh/ssh_config命令校验:
tasks:
- name: Insert/Update configuration using a local file and validate it
blockinfile:
block: "{{ lookup('file', './local/ssh_config') }}"
dest: /etc/ssh/ssh_config
backup: yes
validate: /usr/sbin/sshd -T -f %s
tasks:
- name: Add mappings to /etc/hosts
blockinfile:
path: /etc/hosts
block: |
{{ item.ip }} {{ item.name }}
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
with_items:
- { name: host1, ip: 10.10.1.10 }
- { name: host2, ip: 10.10.1.11 }
- { name: host3, ip: 10.10.1.12 }