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 ~]#