ansible模块
shell模块
[21:18 root@m01 ~]# ansible all -m shell -a "hostname -I|awk '{print \$2}'"
172.16.1.41 | CHANGED | rc=0 >>
172.16.1.41
172.16.1.7 | CHANGED | rc=0 >>
172.16.1.7
172.16.1.31 | CHANGED | rc=0 >>
172.16.1.31
copy 推送文件模块
src=
源 推送数据的全路径
dest=
目标 推送数据的目标路径
owner=
指定推送文件的所有者信息
group=
指定推送文件的用户组信息
mode=
指定推送文件的权限信息
backup=
对传送过去的数据进行备份
content=
批量在服务端文件内添加内容 先清空再增加,与src二选一
src=
dest=
[21:23 root@m01 ~]# ansible all -m copy -a 'src=/etc/hostname dest=/tmp/'
[21:23 root@m01 ~]#
[21:23 root@m01 ~]#
[21:24 root@m01 ~]# ansible all -a 'cat /tmp/hostname'
172.16.1.31 | CHANGED | rc=0 >>
m01
172.16.1.7 | CHANGED | rc=0 >>
m01
172.16.1.41 | CHANGED | rc=0 >>
m01
backup=
把/etc/hostname分发到/tmp下,再把/etc/hosts分发到/tmp/hostname并备份
[21:34 root@m01 ~]# ansible all -m copy -a 'src=/etc/hostname dest=/tmp/'
[21:29 root@m01 ~]# ansible all -m copy -a 'src=/etc/hosts dest=/tmp/hostname backup=yes'
[21:29 root@m01 ~]# \\
[21:29 root@m01 ~]#
[21:30 root@m01 ~]# ansible all -m shell -a 'head /tmp/hostname*'
172.16.1.31 | CHANGED | rc=0 >>
==> /tmp/hostname <==
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
==> /tmp/hostname.8691.2019-05-28@12:41:13~ <==
m01 \\备份的
.....
content=
批量在服务端文件内添加内容 先清空再增加,与src二选一
copy的src参数和content参数相互冲突,只能用一个
[22:08 root@m01 ~]# ansible all -m copy -a 'src=/etc/hostname dest=/tmp/lidao.txt content="oldboy.com"'
172.16.1.7 | FAILED! => {
"changed": false,
"msg": "src and content are mutually exclusive" \\相互冲突
}
172.16.1.41 | FAILED! => {
"changed": false,
"msg": "src and content are mutually exclusive"
}
172.16.1.31 | FAILED! => {
"changed": false,
"msg": "src and content are mutually exclusive"
}
去掉src就行了:
[22:10 root@m01 ~]# ansible all -m copy -a 'dest=/tmp/lidao.txt content="oldboy.com"'
[22:18 root@m01 ~]# ansible all -a 'cat /tmp/lidao.txt'
172.16.1.41 | CHANGED | rc=0 >>
oldboy.com
172.16.1.31 | CHANGED | rc=0 >>
oldboy.com
172.16.1.7 | CHANGED | rc=0 >>
oldboy.com
scripts 模块
先把脚本传输到远端 然后执行
写一个脚本:
[1:43 root@m01 ~]# vim /server/scripts/yum.sh
▽
yum install -y ipvsadm
分发这个脚本:
[08:50 root@m01 ~]# ansible all -m copy -a 'src=/server/scripts/for.sh dest=/server/scripts/for.sh'
分发执行这个脚本:
[21:59 root@m01 ~]# ansible all -m script -a "/server/scripts/yum.sh"
yum 安装模块
name
指定要安装的软件包名
state
要执行的yum动作
installed & present 安装软件包
remove & absent 卸载 关闭或者删除
latest 更新软件包
[22:03 root@m01 ~]# ansible all -m yum -a 'name=sl state=present'
file 文件模块
具有touch mkdir ln rm 的功能
不支持通配符
创建多层目录:directory
[22:19 root@m01 ~]# ansible all -m file -a 'path=/tmp/1/2/3/4 state=directory'
创建多层文件:touch
[22:22 root@m01 ~]# ansible all -m file -a 'path=/tmp/1/2/3/oldboy.txt state=touch'
查看/tmp/目录下是否创建成功:
[22:23 root@m01 ~]# ansible all -a 'tree /tmp/'
172.16.1.41 | CHANGED | rc=0 >>
/tmp/
├── 1
│ └── 2
│ └── 3
│ ├── 4
│ └── oldboy.txt
serivce 服务模块
name=服务名
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #平滑重启服务
enabled
yes 让服务开机自启
no 默认disable
开启crond服务:enabled=yes
[22:33 root@m01 ~]# ansible all -m service -a 'name=crond enabled=yes'
group组模块
name 指定创建的组名
gid 指定组的gid
state
absent 移除远端主机的组
present 创建远端主机的组(默认)
创建guoav1组 指定gid为1113
[22:42 root@m01 ~]# ansible all -m group -a 'name=guoav1 gid=1113 state=present'
user用户模块
name 创建的用户名
uid 指定创建用户的uid
gid 指定创建用户的gid
group 指定用户组名称
groups 指定附加组名称
password 给用户添加密码
shell 指定用户登录shell
create_home 是否创建家目录
创建guoav用户指定uid 1111和gid 1111 ,不创建家目录也不允许登录
先创建组:
[22:42 root@m01 ~]# ansible all -m group -a 'name=guoav gid=1111 state=present'
创建用户:
[22:49 root@m01 ~]# ansible all -m user -a 'name=guoav uid=1111 group=1111 shell=/sbin/nologin create_home=no'
检查一下创建是否成:
[22:49 root@m01 ~]# ansible all -a 'grep guoav /etc/passwd'
172.16.1.7 | CHANGED | rc=0 >>
guoav:x:1111:1111::/home/guoav:/sbin/nologin
172.16.1.31 | CHANGED | rc=0 >>
guoav:x:1111:1111::/home/guoav:/sbin/nologin
172.16.1.41 | CHANGED | rc=0 >>
guoav:x:1111:1111::/home/guoav:/sbin/nologin
crond 定时任务模块
1.检查是否安装ntpdate
[09:32 root@m01 ~]# ansible all -a 'rpm -qa ntpdate'
2.每5分钟同步一次时间:
[09:34 root@m01 ~]# ansible all -m cron -a 'name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
3.再添加一个定时任务
[09:55 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
4.把此定时任务删除:
[09:55 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" state=absent'
查看一下
[09:56 root@m01 ~]# ansible all -a 'crontab -l'
5.再添加一个定时任务
[09:57 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1"'
6.给定时任务添加注释:
[10:00 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=yes'
查看一下
[10:00 root@m01 ~]# ansible all -a 'crontab -l'
172.16.1.31 | CHANGED | rc=0 >>
#Ansible: guoav-date
#*/30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
7.取消注释:
[10:02 root@m01 ~]# ansible all -m cron -a 'name="guoav-date" minute="*/30" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" disabled=no'
查看一下
[10:02 root@m01 ~]# ansible all -a 'crontab -l'
172.16.1.7 | CHANGED | rc=0 >>
#Ansible: guoav-date
*/30 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
远程启动/关闭定时任务服务
mount 挂载模块
1.首先保证nfs01客户端的nfs服务开启
[10:12 root@nfs01 ~]# systemctl is-active rpcbind
active
[10:13 root@nfs01 ~]# systemctl is-active nfs
active
2.将/nfs共享目录挂载到web01服务端上
[10:09 root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs 172.16.1.0/24
/upload 172.16.1.0/24
[10:09 root@web01 ~]# mount -t nfs 172.16.1.31:/nfs/ /mnt/
[10:09 root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.7G 18G 9% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/nfs 19G 1.7G 18G 9% /mnt
测试完把/mnt卸载掉:
umount /mnt
3.用ansible将/nfs挂载到web01上的/upload
1.查看挂载信息
2.挂载 state=mounted
3.查看
[10:27 root@m01 ~]# ansible 172.16.1.7 -a 'showmount -e 172.16.1.31'
172.16.1.7 | CHANGED | rc=0 >>
Export list for 172.16.1.31:
/nfs 172.16.1.0/24
/upload 172.16.1.0/24
[10:26 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/upload",
"opts": "defaults",
"passno": "0",
"src": "172.16.1.31:/nfs"
}
[10:26 root@m01 ~]# ansible 172.16.1.7 -a 'df -h'
172.16.1.7 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
....
172.16.1.31:/nfs 19G 1.7G 18G 9% /upload
4.不挂载,只添加到fstab文件
state=present
[10:46 root@m01 ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/date path=/tmp state=present'
5.卸载,会删除/etc/fstab
state=absent
[11:04 root@m01 ~]# ansible 172.16.1.7 -m mount -a ' src=172.16.1.31:/nfs path=/upload state=absent'