1. 使用lineinfile模块修改文件内容
1.1 修改匹配行,如果不存在则添加
lineinfile
模块能够确保文件中的特定行存在,如果不存在则添加。例如,确保配置文件中设置了 SELinux 为 enforcing 模式:
– name: Ensure SELinux is set to enforcing mode
lineinfile:
path: /etc/selinux/config
regexp: ‘^SELINUX=’
line: SELINUX=enforcing
此任务会检查
/etc/selinux/config
文件中是否有以
SELINUX=
开头的行,如果没有,则添加
SELINUX=enforcing
。
1.2 删除文件中的匹配行
使用
state: absent
参数可以删除文件中所有匹配正则表达式的行。例如,从 sudoers 配置中移除 wheel 组:
– name: Ensure wheel group is not in sudoers
lineinfile:
path: /etc/sudoers
state: absent
regexp: ‘^%wheel’
此任务会删除
/etc/sudoers
文件中所有以
%wheel
开头的行。
1.3 在匹配行前后添加内容
insertbefore
和
insertafter
参数可以在匹配行的前后添加内容。例如,在 Apache 配置文件中添加端口监听:
– name: Ensure the default Apache port is 8080
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: ‘^Listen ‘
insertafter: ‘^#Listen ‘
line: Listen 8080
此任务会在包含
#Listen
的行之后添加
Listen 8080
。
1.4 修改文件内容并更改权限
除了修改文件内容,
lineinfile
模块还可以更改文件的权限和所有权。例如,修改 hosts 文件并设置权限:
– name: Replace a localhost entry with our own and set permissions
lineinfile:
path: /etc/hosts
regexp: ‘^127\.0\.0\.1’
line: 127.0.0.1 localhost
owner: root
group: root
mode: ‘0644’
此任务会替换
/etc/hosts
文件中第一个匹配
127.0.0.1
的行,并设置文件的所有者为 root,组为 root,权限为
0644
。
2. 使用replace模块替换文件内容
2.1 替换文件中匹配的字符串
Ansible的
replace
模块能够根据提供的正则表达式搜索并替换文件中的字符串。此模块在处理文本替换时比
lineinfile
模块更为灵活,因为它可以替换文件中所有匹配到的字符串,而不仅仅是整行。
替换操作
:例如,如果要将文件
/tmp/test.txt
中所有出现的字符串
zsf
替换为
zhangshoufu
,可以使用以下任务:
“`yaml
name: ‘替换zsf字符串为zhangshoufu’
replace:
path: /tmp/test.txt
regexp: ‘zsf’
replace: ‘zhangshoufu’
”
此任务会搜索整个文件,将所有
zsf
字符串替换为
zhangshoufu`。
2.2 注释文件中匹配的行
replace
模块也可以用来注释文件中的行。通过将匹配到的行前添加注释符号,可以快速禁用配置文件中的某些行。
注释操作
:如果要注释
/etc/apache2/sites-available/default.conf
文件中
NameVirtualHost [*]
行之后的所有内容,可以使用以下任务:
“`yaml
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’
”
此任务会在
NameVirtualHost [*]
之后的所有行前添加
#`,实现注释效果。
2.3 修改文件属性和权限
在替换文件内容的同时,
replace
模块也允许修改文件的属性和权限。这对于确保文件符合特定的安全策略非常有用。
属性修改
:例如,如果要在替换操作后修改
/home/jdoe/.ssh/known_hosts
文件的所有者、组和权限,可以使用以下任务:
“`yaml
name: Supports common file attributes
replace:
path: /home/jdoe/.ssh/known_hosts
regexp: ‘^old.host.name[^\n]*\n’
owner: jdoe
group: jdoe
mode: ‘0644’
”
此任务在替换文件内容的同时,将文件的所有者设置为
jdoe
,组设置为
jdoe
,并将文件权限设置为
0644`。
3. 使用blockinfile模块操作文件内容
3.1 在文件末尾添加多行内容
blockinfile
模块允许将多行文本添加到文件的末尾,如果文件不存在,则会创建该文件。这对于配置文件的批量更新非常有用。
– name: Add multiple lines to the end of a file
blockinfile:
path: /etc/hosts
block: |
192.168.1.100 ansible-managed-host
192.168.1.101 another-host
3.2 备份文件并在末尾插入内容
在修改文件之前,可以使用
backup
参数来备份原始文件,确保在出现错误时可以恢复。
– name: Backup and append content to a file
blockinfile:
path: /etc/ssh/sshd_config
block: |
UseDNS no
GSSAPIAuthentication no
backup: yes
3.3 在特定标记后插入内容
使用
marker
参数可以在文件中的特定内容之后插入新的文本块,这对于添加配置注释或组织配置文件结构非常有用。
– name: Insert content after a specific marker
blockinfile:
path: /etc/nginx/nginx.conf
marker: “# {mark} ANSIBLE MANAGED BLOCK”
block: |
server {
listen 80;
server_name example.com;
}
insertafter: “http {”
3.4 移除特定标记的内容
如果
block
参数为空,并且指定了
marker
,则
blockinfile
模块可以移除标记的内容,这可以用来清理或撤销之前的配置更改。
– name: Remove content surrounded by markers
blockinfile:
path: /etc/nginx/nginx.conf
marker: “# {mark} ANSIBLE MANAGED BLOCK”
block: “”
2. ansible 修改文件内容
2.1 使用 lineinfile 模块
lineinfile
是 Ansible 中用于管理文件中特定行的一个模块。它可以确保文件中存在或不存在特定的行,或者在匹配的行前后添加新的内容。
添加或修改行
:如果文件中已存在与
regexp
匹配的行,
lineinfile
模块可以将其替换为
line
参数指定的行。如果不存在匹配行,则会添加新的一行。
删除行
:设置
state
参数为
absent
可以删除所有匹配
regexp
的行。
行插入
:使用
insertafter
或
insertbefore
参数可以在匹配行的前面或后面插入新的行。
2.2 使用 replace 模块
replace
模块允许在整个文件中查找和替换文本。它使用正则表达式来定位要替换的文本,并可以替换所有匹配的实例。
替换文本
:通过
regexp
参数定义要查找的文本模式,
replace
参数定义新的文本内容。
范围限定
:可以使用
after
和
before
参数限定替换的范围,仅在这些参数指定的文本之后或之前进行替换。
2.3 使用 blockinfile 模块
blockinfile
模块用于在文件中插入或更新多行文本块。它提供了一种方便的方式来管理较大的文本段落。
插入文本块
:
block
参数允许定义一个多行字符串,该字符串将被插入文件中。
标记管理
:
marker
参数可以用来在插入的文本块前后添加标记,以便于识别和管理。
条件插入
:通过
insertafter
或
insertbefore
参数,可以控制文本块插入的位置。
文件验证
:
validate
参数允许在插入文本后运行一个命令来验证文件的完整性或正确性。
2.4 使用 file 模块
虽然
file
模块不直接修改文件内容,但它可以用来创建新文件或修改文件属性,如权限和所有者。
创建文件
:设置
state
参数为
touch
可以创建一个新文件或更新现有文件的时间戳。
修改权限
:
mode
参数可以设置文件的权限,确保文件具有正确的访问控制。
更改所有者
:
owner
和
group
参数可以改变文件的所有者和所属组,这对于权限管理非常重要。
在实际应用中,选择哪个模块取决于具体的需求和场景。例如,如果需要修改配置文件中的单个参数,
lineinfile
或
replace
模块可能更合适。而对于需要插入较大文本块的情况,
blockinfile
模块则更为方便。而
file
模块则更多用于文件的创建和属性管理。