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 ~]#
相关推荐
TomEval8 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill9 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
Thanks_ks10 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP10 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
kyle~10 小时前
嵌入式 SoC 系统
linux·c++·嵌入式·计算系统
yiangL_11 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
lula88812 小时前
PVC-U、PVC-C、PVC-O 差一个字母,差的是三种增强路线
python
人生!?12 小时前
Linux 从入门到进阶 | 基础语法
linux·笔记·教程
数字工作台13 小时前
给 LangGraph 加一道可审计记忆:替换默认 Checkpointer,实现防篡改 + 崩溃不丢数据
python·agent
DKPT13 小时前
Python 的 Web 框架:选型选错的人,都踩了同一个坑
python