ansible 常用模块
1.ansible 常用模块详解
ansible常用模块有:
- ping
- yum
- template
- copy
- service
- raw
- command
- shell
- script
ansible常用模块command、shell的区别: - shell调用/bin/sh指令执行
- command模块不是调用shell指令,所以没有bash的环境变量
2. 模块 ping
[root@my ansible]# ansible all -m ping //不涉及参数
192.168.47.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
3.模块 command
//ansible默认使用command模块
//用于在远程主机上执行命令,不能使用管道符和重定向
[root@my ansible]# ansible all -a 'hostname' //默认使用command模块
192.168.47.147 | CHANGED | rc=0 >>
node1[root@my ansible]# ansible all -m command -a 'hostname'
192.168.47.147 | CHANGED | rc=0 >>
node1
//查看受控主机/tmp内容
[root@node1 tmp]# rm -rf *
[root@node1 tmp]# ls
//输入my写入/tmp/my文件 ,命令执行成功
[root@my ansible]# ansible all -a 'echo "my" > /tmp/my'
192.168.47.147 | CHANGED | rc=0 >>
my > /tmp/my
//并没有my文件
[root@node1 tmp]# ls
[root@node1 tmp]#
//使用管道符不成功
[root@my ansible]# ansible all -a "df -h |awk '{print $4}'"
192.168.47.147 | FAILED | rc=1 >>
df: '|awk': 没有那个文件或目录
df: '{print }': 没有那个文件或目录non-zero return code
4. 模块 raw
//raw用于在远程主机上执行命令,支持管道符和重定向
//输出my在受控主机/tmp/my下
[root@my ansible]# ansible all -m raw -a 'echo "my" > /tmp/my'
192.168.47.147 | CHANGED | rc=0 >>
Shared connection to 192.168.47.147 closed.
//执行成功,能看见
[root@node1 tmp]# ls
my
//管道符过滤my
[root@my ansible]# ansible all -m raw -a "ls /root"
192.168.47.147 | CHANGED | rc=0 >>
公共 视频 文档 音乐 anaconda-ks.cfg my
模板 图片 下载 桌面 initial-setup-ks.cfg
Shared connection to 192.168.47.147 closed.
[root@my ansible]# ansible all -m raw -a "ls /root | grep my"
192.168.47.147 | CHANGED | rc=0 >>
my
Shared connection to 192.168.47.147 closed.
5. 模块 shell
//用于在受控机上执行受控机上的脚本,也可直接在受控机上执行命令
//支持管道符和重定向
//在受控节点写一个脚本
[root@node1 ~]# cd /tmp
[root@node1 tmp]# ls
my
[root@node1 tmp]# vi test.sh
[root@node1 tmp]# cat test.sh
#!/bin/bash
echo"my"
[root@node1 tmp]# chmod +x test.sh
[root@node1 tmp]# ll
总用量 8
-rw-r--r-- 1 root root 3 7月 18 15:52 my
-rwxr-xr-x 1 root root 21 7月 18 16:01 test.sh
//使用shell、command执行脚本成功
[root@my ansible]# ansible all -m command -a '/tmp/test.sh'
192.168.47.147 | CHANGED | rc=0 >>
my
[root@my ansible]# ansible all -m shell -a "/tmp/test.sh"
192.168.47.147 | CHANGED | rc=0 >>
my
//shell可重定向覆盖原来文件内容
[root@my ansible]# ansible all -m shell -a 'echo "shabi" > /root/my' //输入shabi
192.168.47.147 | CHANGED | rc=0 >>
[root@node1 ~]# cat my //查看
shabi
//万能模块,除了需要交互的命令以外,几乎所有命令都可以执行,只能执行静态命令,,不具备幂等性
[root@my ansible]# ansible all -m shell -a 'top'
^C [ERROR]: User interrupted execution
[root@my ansible]# ansible all -m shell -a 'ps'
192.168.47.147 | CHANGED | rc=0 >>
PID TTY TIME CMD
204228 pts/2 00:00:00 sh
204249 pts/2 00:00:00 platform-python
204251 pts/2 00:00:00 ps
6. 模块 script
//script用于受控主机上执行主控机上的脚本
//在主控机上创建一个目录写一个脚本
[root@my ansible]# mkdir /scripts
[root@my ansible]# cd /scripts
[root@my scripts]# ls
[root@my scripts]# vim test.sh
[root@my scripts]# vim test.sh
[root@my scripts]# chmod +x ./test.sh
[root@my scripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 19 7月 18 16:26 test.sh
[root@my scripts]# cat test.sh
#!/bin/bash
df -h
//执行脚本成功,脚本必须写绝对路径
[root@my scripts]# ansible all -m script -a "/scripts/test.sh"
192.168.47.147 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.47.147 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.47.147 closed."
],
"stdout": "文件系统 容量 已用 可用 已用% 挂载点\r\ndevtmpfs 872M 0 872M 0% /dev\r\ntmpfs 901M 0 901M 0% /dev/shm\r\ntmpfs 901M 21M 880M 3% /run\r\ntmpfs 901M 0 901M 0% /sys/fs/cgroup\r\n/dev/mapper/rhel-root 17G 4.9G 13G 29% /\r\n/dev/sr0 7.9G 7.9G 0 100% /mnt/cdrom\r\n/dev/nvme0n1p1 1014M 229M 786M 23% /boot\r\ntmpfs 181M 1.2M 179M 1% /run/user/42\r\ntmpfs 181M 4.0K 181M 1% /run/user/0\r\n",
"stdout_lines": [
"文件系统 容量 已用 可用 已用% 挂载点",
"devtmpfs 872M 0 872M 0% /dev",
"tmpfs 901M 0 901M 0% /dev/shm",
"tmpfs 901M 21M 880M 3% /run",
"tmpfs 901M 0 901M 0% /sys/fs/cgroup",
"/dev/mapper/rhel-root 17G 4.9G 13G 29% /",
"/dev/sr0 7.9G 7.9G 0 100% /mnt/cdrom",
"/dev/nvme0n1p1 1014M 229M 786M 23% /boot",
"tmpfs 181M 1.2M 179M 1% /run/user/42",
"tmpfs 181M 4.0K 181M 1% /run/user/0"
]
}
7. 模块 template
//用于生成一个模板,并将其传输至远程主机上
[root@my ansible]# pwd
/etc/ansible
[root@my ansible]# ls
ansible.cfg hosts inventory
[root@my ansible]# ansible all -m template -a "src=/etc/ansible/inventory dest=/tmp/inventory" //src源,dest目标
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "29bc828458159363a7b553f0ec7e0b7244eb35db",
"dest": "/tmp/inventory",
"gid": 0,
"group": "root",
"md5sum": "e72855b83a9f232e5c046013d7806e03",
"mode": "0644",
"owner": "root",
"size": 64,
"src": "/root/.ansible/tmp/ansible-tmp-1626597339.368368-237015-245063678396521/source",
"state": "file",
"uid": 0
}
//在受控主机上查询到
[root@node1 tmp]# ls
ansible_command_payload_ee4k10ba ansible_command_payload_l3hx_zb6 inventory my test.sh
[root@node1 tmp]#
8. 模块 copy
//复制文件至远程主机
[root@my ansible]# ls
ansible.cfg hosts inventory my myy myyy roles
[root@my ansible]# ansible all -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/"192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "0c90f8acee99e8bbbc6f465ba0ee787a1e1cac96",
"dest": "/tmp/ansible.cfg"
//在受控主机上查看ansible.cfg
[root@node1 tmp]# ls
ansible.cfg ansible_command_payload_l3hx_zb6 my
ansible_command_payload_ee4k10ba inventory
9. 模块 yum
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
name:要管理的包名
state:要进行的操作
state常用的值:
latest:安装软件
installed:安装软件
present:安装软件
removed:移除卸载软件
absent:卸载软件
//查询vsftpd包
[root@node1 tmp]# rpm -qa | grep vsftpd
//安装。用yum和dnf都行
[root@my ansible]# ansible all -m dnf -a "name=vsftpd state=present"
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-31.el8.x86_64"
]
}
//查询
[root@node1 tmp]# rpm -qa | grep vsftpd
vsftpd-3.0.3-31.el8.x86_64
//卸载
[root@my ansible]# ansible all -m dnf -a "name=vsftpd state=absent"
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": ""
10. group、user 模块
//创建一个名字为myy的组,gid为922的系统账户
[root@my ansible]# ansible all -m group -a 'name=myy state=present gid=922 system=yes'
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 922,
"name": "myy",
"state": "present",
"system": true
}
//查询myy组
[root@node1 ~]# grep myy /etc/group
myy:x:922:
//删除组
[root@my ansible]# ansible all -m group -a 'name=myy state=absent'
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "myy",
"state": "absent"
}
//创建一个名为my1的用户,uid为922,gid为myy,为系统用户,不创建 家目录no
[root@my ansible]# ansible all -m user -a "name=my1 state=present uid=922 group=myy system=yes create_home=no"
192.168.47.147 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
//查询用户
[root@node1 ~]# id my1
uid=922(my1) gid=922(myy) 组=922(myy)
[root@node1 ~]# grep my1 /etc/passwd
my1:x:922:922::/home/my1:/bin/bash
//修改shell
[root@my ansible]# ansible all -m user -a "name=my1 state=present uid=922 group=myy system=yes create_home=no shell=/sbin/nologin"
192.168.47.147 | CHANGED => {
"ansible_facts": {
//查询
[root@node1 ~]# grep my1 /etc/passwd
my1:x:922:922::/home/my1:/sbin/nologin
//删除用户
[root@node1 ~]# ll /home/
总用量 4
drwx------. 17 my my 4096 7月 16 17:48 my
[root@my ansible]# ansible all -m user -a "name=my state=absent"
192.168.47.147 | CHANGED => {
"ansible_facts": {
//删除了用户但家目录还存在
[root@node1 ~]# id my
id: “my”:无此用户
[root@node1 ~]# ll /home/
总用量 4
drwx------. 17 1000 1000 4096 7月 16 17:48 my
//删除用户的同时删除家目录
[root@my ansible]# ansible all -m user -a "name=my state=absent remove=yes" /remove家目录
192.168.47.147 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"name": "my",
"state": "absent"
}
//查询已删除
[root@node1 ~]# ll /home/
总用量 0
11. 模块 service
//开启服务
[root@my ansible]# ansible all -m service -a 'name=vsftpd state=started'
192.168.47.147 | CHANGED => {
"ansible_facts": {
//设置开机自启
[root@my ansible]# ansible all -m service -a 'name=vsftpd state=started enabled=yes'
192.168.47.147 | CHANGED => {
"ansible_facts": {
//设置开机不自启
[root@my ansible]# ansible all -m service -a 'name=vsftpd state=started enabled=yes'
192.168.47.147 | CHANGED => {
"ansible_facts": {
12. 模块 file
//改变文件属主
[root@my ansible]# ansible all -m file -a 'path=/tmp/my owner=my' //owner属主、path指定修改的文件
192.168.47.147 | CHANGED => {
"ansible_facts": {
[root@node1 ~]# ll /tmp/
-rw-r--r-- 1 my root 3 7月 18 15:52 my
//改变文件属主和属组
[root@my ansible]# ansible all -m file -a 'path=/tmp/my owner=my group=my'
192.168.47.147 | CHANGED => {
"ansible_facts": {
[root@node1 ~]# ll /tmp/
-rw-r--r-- 1 my my 3 7月 18 15:52 my
//修改权限
[root@my ansible]# ansible all -m file -a 'path=/tmp/my owner=my group=my mode=622' //mode权限
192.168.47.147 | CHANGED => {
"ansible_facts": {
[root@node1 ~]# ll /tmp/
-rw--w--w- 1 my my 3 7月 18 15:52 my
//创建一个空文件,再执行一遍可刷新时间轴
[root@my ansible]# ansible all -m file -a 'path=/tmp/abc state=touch'
[root@node1 tmp]# ll
-rw-r--r-- 1 root root 0 7月 18 18:39 abc
//创建一个目录
[root@my ansible]# ansible all -m file -a 'path=/tmp/my state=directory'
//创建软连接
[root@my ansible]# ansible all -m file -a 'path=/tmp/yiyi src=/tmp/my state=link'
[root@node1 tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 18 18:39 abc
drwxr-xr-x 2 root root 6 7月 18 18:42 my
lrwxrwxrwx 1 root root 7 7月 18 18:46 yiyi -> /tmp/my
//创键硬链接,目录不可以创建硬链接可以创软连接,只有文件才可以
[root@my ansible]# ansible all -m file -a 'path=//tmp/erer src=/tmp/abc state=hard'
[root@node1 tmp]# ll
总用量 0
-rw-r--r-- 2 root root 0 7月 18 18:39 abc
-rw-r--r-- 2 root root 0 7月 18 18:39 erer
drwxr-xr-x 2 root root 6 7月 18 18:42 my
lrwxrwxrwx 1 root root 7 7月 18 18:46 yiyi -> /tmp/my
//删除文件,path表示路径
[root@my ansible]# ansible all -m file -a 'path=/tmp/abc state=absent'
//修改文件权限
[root@my ansible]# ansible all -m file -a 'path=/tmp/my mode=0700'
13. 模块 lineinfile
//修改selinux为disabled,^代表以什么开头
[root@my ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
//把以下内容下入my文件里
[root@node1 tmp]# echo -e "my\n922\nawm" > /tmp/my
[root@node1 tmp]# cat my
my
922
awm
//把文件my里的my删除
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/my regexp="^my" state=absent'
//删除文件里的以my开头的行,去掉^代表把所有带my的行删除
[root@node1 tmp]# cat my
my
my
my
he
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/my regexp="^my" state=absent'
[root@node1 tmp]# cat my
my
my
he
//在my文件里,在以hehe开头的行的后面插入my这一行
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/my insertafter="^hehe" line="80"'
[root@node1 tmp]# cat my
hello world
hehe
80
xixi
hehe
//默认在最后一行加入8080
[root@node1 tmp]# cat my
hello world
80
80
xixi
80
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/my insertafter="80" regexp="hehe" line="8080"'
[root@node1 tmp]# cat my
hello world
80
80
xixi
80
8080
//匹配以xixi开头的xixi,并把xixi修改为hehe
[root@node1 tmp]# cat my
hello world
80
80
xixi
80
8080
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/my regexp="^xixi" insertafter="xixi" line="hehe"' //insertafter插入的意思
[root@node1 tmp]# cat my
hello world
80
80
hehe
80
8080
//创建一个文件,文件里有三行内容
[root@my ansible]# ansible all -m lineinfile -a 'path=/tmp/yiyi line="hello world\nhehe\nxixi" create=yes'
[root@node1 tmp]# cat yiyi
hello world
hehe
xixi
14.模块 blockinfile
blockinfile 在文件中插入”一段文本”,这段文本被标记过,方便在以后的操作中可以通过”标记”找到这段文本,然后修改或者删除它
参数:
- path:指定文件。
- backup :是否在修改文件之前对文件进行备份。
- create :创建文件
[root@my ansible]# ansible all -m blockinfile -a 'path=/tmp/mymy block="systemctl start php\nsystemctl start httpd" create=yes' 192.168.47.147 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "msg": "File created" } [root@node1 tmp]# cat mymy # BEGIN ANSIBLE MANAGED BLOCK systemctl start php systemctl start httpd # END ANSIBLE MANAGED BLOCK