Ansible 常用模块详解:lineinfile、replace、get_url实战

Ansible常用模块

lineinfile模块

复制代码
-m lineinfile 
    和sed类型。提供修改或者删除文件内容的操作
指令参数 选项 说明
path 指定被控端需要操作的文件
regexp 使用正则表示式匹配对应的行:当前行替换
insertbefore 使用正则表示式匹配对应的行:之前插入
insertafter 使用正则表示式匹配对应的行:之后插入
line 修改的内容:必填
state absent、 present 文件操作类型
create yes、 no 当文件不存在的时候,是否创建文件
复制代码
[root@ansible ~]# vi a.txt
[root@ansible ~]# ansible webservers -m copy -a 'src=a.txt dest=/opt/a.txt'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "2534916a1d4c43f77e963e63eb5a39a97cfbc1f6",
    "dest": "/opt/a.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "b4b86eec5290fedd3137ab3f6b84531c",
    "mode": "0644",
    "owner": "root",
    "size": 9,
    "src": "/root/.ansible/tmp/ansible-tmp-1774578264.002327-2289-142880024380021/source",
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/a.txt'
192.168.92.20 | CHANGED | rc=0 >>
yunhhhhh
[root@ansible ~]# ansible webservers -m lineinfile -a 'path=/opt/a.txt regexp="^yun" line="这里是被替换的数据"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/a.txt'
192.168.92.20 | CHANGED | rc=0 >>
这里是被替换的数据
[root@ansible ~]# ansible webservers -m lineinfile -a 'path=/opt/a.txt insertbefore="^这里是" line="这里是在之前加入的内容"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/a.txt'
192.168.92.20 | CHANGED | rc=0 >>
这里是在之前加入的内容
这里是被替换的数据
[root@ansible ~]# ansible webservers -m lineinfile -a 'path=/opt/a.txt insertafter="^这里是" line="这里是在之后加入的内容"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/a.txt'
192.168.92.20 | CHANGED | rc=0 >>
这里是在之前加入的内容
这里是被替换的数据
这里是在之后加入的内容
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/b.txt'
192.168.92.20 | FAILED | rc=1 >>
cat: /opt/b.txt: No such file or directorynon-zero return code
[root@ansible ~]# ansible webservers -m lineinfile -a 'path=/opt/b.txt line="aaaaaaaaaaaaa" create=yes '
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/b.txt'
192.168.92.20 | CHANGED | rc=0 >>
aaaaaaaaaaaaa

replace 模块

复制代码
-m replace 
    做文本内容替换,针对的式内容
指令参数 选项 说明
path 指定被控端需要操作的文件
regexp 使用正则表达式匹配对应内容:当前替换
before 使用正则表达式匹配对应内容:之前插入
after 使用正则表达式匹配对应内容:之后插入
replace 需要修改的内容:必写
复制代码
[root@ansible ~]# vi replace.txt
[root@ansible ~]# ansible webservers -m copy -a  'src=replace.txt dest=/opt/replace.txt'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "c2bbfd19d8f44457ecd0007669d34df1e74eca5b",
    "dest": "/opt/replace.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "ddc286e4525d2fd989ab5f014d6616a7",
    "mode": "0644",
    "owner": "root",
    "size": 41,
    "src": "/root/.ansible/tmp/ansible-tmp-1774581701.2643402-22837-30944122212576/source",
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
dfajfhdaj
fajfia
ofkafea
lfdaf
qfads
yun
[root@ansible ~]# ansible webservers -m replace -a 'path=/opt/replace.txt regexp="yun" replace="YUN"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
dfajfhdaj
fajfia
ofkafea
lfdaf
qfads
YUN
[root@ansible ~]# ansible webservers -m shell -a "echo "77777777777777799999999999" >> /opt/replace.txt"
192.168.92.20 | CHANGED | rc=0 >>

[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
dfajfhdaj
fajfia
ofkafea
lfdaf
qfads
YUN
77777777777777799999999999
[root@ansible ~]# ansible webservers -m replace -a 'path=/opt/replace.txt regexp="of.*" replace=of###'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
dfajfhdaj
fajfia
of###
lfdaf
qfads
YUN
77777777777777799999999999
[root@ansible ~]# ansible webservers -m replace -a 'path=/opt/replace.txt after="lfdaf" regexp="qf" replace="QF"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
dfajfhdaj
fajfia
of###
lfdaf
QFads
YUN
77777777777777799999999999
[root@ansible ~]# ansible webservers -m replace -a 'path=/opt/replace.txt before="lfdaf" regexp="df" replace="DF"'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
[root@ansible ~]# ansible webservers -m shell -a 'cat /opt/replace.txt'
192.168.92.20 | CHANGED | rc=0 >>
DFajfhdaj
fajfia
of###
lfdaf
QFads
YUN
77777777777777799999999999

get_url 模块

复制代码
-m get_url 
    通过网络下载文件到被控端
指令参数 选项 说明
url 资源文件在互联网的地址:http或者https
dest 被控端保存路径(最好写绝对路径)
checksum MD5、sha256 下载之后,对文件做资源验证
timeout 10 请求超时时间(秒)
复制代码
[root@ansible ~]# ansible webservers -m get_url -a 'url=https://repo1.maven.org/maven2/ dest=/opt'
192.168.92.20 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "0bde988cca7c87280c6720fc1a10e7a3c868c0da",
    "dest": "/opt/index.html",
    "elapsed": 4,
    "gid": 0,
    "group": "root",
    "md5sum": "39f420a226a4c296c47688fc14b60ce0",
    "mode": "0644",
    "msg": "OK (unknown bytes)",
    "owner": "root",
    "size": 130434,
    "src": "/root/.ansible/tmp/ansible-tmp-1774599815.2290885-4628-87229399172733/tmpuyb93_9z",
    "state": "file",
    "status_code": 200,
    "uid": 0,
    "url": "https://repo1.maven.org/maven2/"
} 
[root@ansible ~]# ansible webservers -m shell -a 'ls /opt'
192.168.92.20 | CHANGED | rc=0 >>
a.txt
b.txt
cp.txt
cp.txt.2585.2026-03-26@20:21:13~
index.html
replace.txt
xxx.txt
[root@ansible ~]#
相关推荐
我喜欢山,也喜欢海2 小时前
Java和go在并发上的表现为什么不一样
java·python·golang
Wenzar_3 小时前
**零信任架构下的微服务权限控制:用Go实现基于JWT的动态访问策略**在现代云原生环境中,
java·python·微服务·云原生·架构
不是起点的终点3 小时前
【实战】Python 一键生成数据库说明文档(对接阿里云百炼 AI,输出 Word 格式)
数据库·python·阿里云
2301_813599555 小时前
Go语言怎么做秒杀系统_Go语言秒杀系统实战教程【实用】
jvm·数据库·python
--fancy9 小时前
股票预测情感分析研究案例分析
python
shughui9 小时前
PyCharm 完整教程(旧版本卸载+旧/新版本下载安装+基础使用,2026最新版附安装包)
ide·python·pycharm
爱学习的小囧9 小时前
ESXi 8.0 原生支持 NVMe 固态硬盘吗?VMD 配置详解教程
linux·运维·服务器·esxi·esxi8.0
坚持就完事了10 小时前
Linux中的变量
linux·运维·服务器
小糖学代码10 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
yejqvow1210 小时前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python