在ansible中是指需要快速执行一条命令,并且不需要保存的命令,对于复杂的命令则为playbook

ansible注意事项->提示颜色信息说明

翔黄色:对远程节点进行相应修改
帽子绿:对远程节点不进行相应的修改,或者只是对远程节点信息进行查看
深红色:操作执行命令有异常
浅紫色:表示对命令执行发出警告信息(可能存在问题,给你一下建议)

模块实现的功能:

安装软件包
修改配置文件
创建程序用户组
创建目录,并修改所属组和权限
启动服务
挂载
测试

1 command命令模块

# 默认模块,执行命令
[root@linux-node1 ~]# ansible sunrise -a "hostname"

# 如果需要一些管道操作,则使用shell
[root@linux-node1 ~]# ansible sunrise -m shell -a "df -h | grep /$" -f 3

# -f = forks /etc/ansible/ansible.cfg  #结果返回的数量 

command与shell的区别(注意):
1.command只能调用一条指令
2.shell可以使用管道
3.不支持别名

举例:同一创建用户sun,并修改密码为123

[root@linux-node1 ~]# ansible sunrise -m shell -a "useradd sun"
[root@linux-node1 ~]# ansible sunrise -m command -a "useradd sun"

[root@linux-node1 ~]# ansible sunrise -m shell -a "echo 123|passwd --stdin sun"

2 script脚本模块

# 编写脚本
[root@linux-node1 ~]# mkdir -p /server/scripts
[root@linux-node1 ~]# cat /server/scripts/yum.sh
#!/usr/bin/bash
yum install -y iftop
# 在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@linux-node1 ~]# ansible sunrise -m script -a "/server/scpipts/yum.sh"

例1:
1)写个脚本创建,创建用户,并配置密码

[root@linux-node1 ~]# mkdir -p /server/scripts/
[root@linux-node1 ~]# cd /server/scripts/
[root@linux-node1 scripts]# cat >1.sh <<EOD
#!/bin/bash
useradd zhao
echo 123|passwd --stdin zhao &>/dev/null && echo "创建密码成功"
EOD

2)运行脚本

[root@linux-node1 scripts]# ansible all -m script -a "/server/scripts/1.sh"

3 yum安装软件模块

[root@linux-node1 ~]# ansible sunrise -m yum -a "name=httpd state=installed"

name  --指定要安装的软件包名字,如果有多个,用","隔开
state --指定要用yum的方法
    installed,present  --安装软件包
    removed,absent       --移除软件包
    latest               --安装最新软件包

4 copy文件模块

#copy推送文件模块
[root@linux-node1 ~]# ansible sunrise -m copy -a "src=/etc/hosts dest=/tmp/test.txt"

#在推送覆盖远程端文件前,对远端已有文件进行备份,按照时间信息备份
[root@linux-node1 ~]# ansible sunrise -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"

#直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
[root@linux-node1 ~]# ansible sunrise -m copy -a "content='bgx' dest=/tmp/sunrise"

参数:

src           --- 推送数据的源文件信息
dest          --- 推送数据的目标路径
backup        --- 对推送传输过去的文件,进行备份
content       --- 直接批量在被管理端文件中添加内容
group         --- 将本地文件推送到远端,指定文件属组信息
owner         --- 将本地文件推送到远端,指定文件属主信息
mode          --- 将本地文件推送到远端,指定文件权限信息

案例1:批量推送hosts文件并备份

[root@linux-node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

#############################################
192.168.6.240    linux-node1.98yz.cn    node01
192.168.6.241    linux-node2.98yz.cn    node02
192.168.6.242    linux-node3.98yz.cn    node03
192.168.6.243    linux-node4.98yz.cn    node04
192.168.6.243    linux-node5.98yz.cn    node05

分发hosts

[root@linux-node1 ~]# ansible sunrise -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"

案例2:添加rsync认证文件和rsync客户端密码文件
添加rsync服务端认证文件:

[root@linux-node1 ~]# ansible backup -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password owner=root group=root mode=600"

验证:

[root@linux-node1 ~]# ansible backup -m shell -a "ls -l /etc/rsync.password"

[root@linux-node1 ~]# ansible backup -m shell -a "cat /etc/rsync.password"

添加rsync客户端密码文件:

[root@linux-node1 ~]# ansible sunrise -m copy -a "content='1' dest=/etc/password.pass owner=root group=root mode=600"

验证:

[root@linux-node1 ~]# ansible sunrise -m shell -a "ls -l /etc/password.pass"
[root@linux-node1 ~]# ansible sunrise -m shell -a "cat /etc/password.pass"

5 service服务模块

[root@linux-node1 ~]# ansible sunrise -m service -a "name=crond state=stopped enabled=yes"

name        --- 定义要启动服务的名称
state       --- 指定服务状态是停止或是运行,停止和运行指令要写成过去时
    started       --- 启动
    stopped     --- 停止
    restarted    --- 重启
    reloaded    --- 重载
enabled         --- 是否让服务开启自启动

案例:启动web的httpd服务,并添加一个首页文件,通过浏览器可以访问

[root@linux-node1 ~]# ansible web -m service -a "name=httpd state=started enabled=yes"

[root@linux-node1 ~]# ansible web -m copy -a "content='welcome to sunsrise' dest=/var/www/html/index.html"

6 file配置模块

[root@linux-node1 ~]# ansible sunrise -m file -a "path=/tmp/sunrise state=diretory"
[root@linux-node1 ~]# ansible sunrise -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
[root@linux-node1 ~]# ansible sunrise -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"
path        --- 指定远程主机目录或文件信息
src            --- 源文件路径
recurse     --- 递归授权  recurse=yes
mode        --- 设置文件或目录权限
owner       --- 设置文件或目录属主信息
group       --- 设置文件或目录属组信息
state
    directory   --- 在远端创建目录
    touch       --- 在远端创建文件
    link        --- link或hard表示创建链接文件
    absent      --- 表示删除文件或目录
    file        --- 修改文件属性(默认)

例1:创建目录/data和/backup,属主666,属组666

[root@linux-node1 ~]# ansible all -m file -a "path=/data owner=666 group=666 recurse=yes state=directory"

例2:创建文件/etc/rsync.password,权限600

ansible all -m file -a "path=/etc/rsync.password mode=600 state=touch"

例3:删除data目录

[root@linux-node1 ~]# ansible all -m file -a "path=/data state=absent"

例4:对/etc/hosts做个软连接,到/tmp/hosts

[root@linux-node1 ~]# ansible all -m file -a "src=/etc/hosts path=/tmp/hosts state=link"

总结:file模块仅适合创建目录,修改所属和权限,创建链接,除开这些操作其他文件管理都通过copy模块实现

7 group模块

name            --- 指定创建的组名
gid             --- 指定组的gid
state
    absent      --- 移除远端主机的组
    present     --- 创建远端主机的组(默认)

例1:创建组名为www,指定gid为666

[root@linux-node1 ~]# ansible sunrise -m group -a "name=www gid=666"

例2:修改www组的gid为888

[root@linux-node1 ~]# ansible all -m group -a "name=www gid=888"        -- all:所有机器 

例3:删除www组

[root@linux-node1 ~]# ansible all -m group -a "name=www gid=888 state=absent"

8 user模块

[root@linux-node1 ~]# echo "bgx"| openssl passwd -1 -stdin 
$1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb.

[root@linux-node1 ~]# ansible sunrise -m user -a 'name=xlw password="$1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb."'

创建oldgirl,设定uid为888,并加入gid为888

[root@linux-node1 ~]# ansible sunrise -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin create_home=no"

随机生成加密字符串(-1使用MD5进行加密 -stdin 非交互式 -salt 加密参数)

[root@linux-node1 ~]# echo "bgx" | openssl passwd -1 -stdin

固定加密字符串

[root@linux-node1 ~]# echo "123"| openssl passwd -1 -stdin -salt 'salt'

创建普通用户,并配置对应的用户密码

[root@linux-node1 ~]# echo "bgx" | openssl passwd -1 -stdin
$1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb.

[root@linux-node1 ~]# ansible sunrise -m user -a 'name=xlw password="$1$765yDGau$diDKPRoCIPMU6KEVEaPTZ0"'

参数

name            --- 指定用户名
uid             --- 指定用户的uid
group           --- 指定用户组名称或gid
groups          --- 指定附加组名称
password        --- 给用户添加密码,不能使用明文,需用openssl加密后的密码
shell           --- 指定用户登录shell,/bin/shell(默认),/sbin/nologin
create_home     --- 创建用户时,是否创建家目录,create_home=no
state
    absent      --- 移除远端主机的组
    present     --- 创建远端主机的组(默认)

例1:创建一个用户lyz,指定uid 60000,gid 666, 并设密码为123
注意:password不能使用明文,需要使用openssl加密后的密码,
password的加密值需要使用双引号
登陆的时候用明文密码登陆

# 随机生成加密字符串 -1使用MD5进行加密,-stdin 非交互式
[root@linux-node1 ~]# echo "123"| openssl passwd -1 -stdin
$1$/0ImVYwP$9ZKBa6nLapWApaDB/S8UT1
[root@linux-node1 ~]# ansible backup -m user -a 'name=lyz uid=60000 group=666 password="$1$/0ImVYwP$9ZKBa6nLapWApaDB/S8UT1"'

例2:创建一个程序www,指定uid 666 gid 666 不让登陆 不创建家目录

[root@linux-node1 ~]# ansible all -m group -a "name=www gid=666"
[root@linux-node1 ~]# ansible all -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"

例3:删除lyz用户

[root@linux-node1 ~]# ansible backup -m user -a "name=lyz uid=60000 group=666  state=absent"
[root@linux-node1 ~]# ansible backup -m user -a "name=lyz state=absent"  #推荐这一种

9 crond模块

正常使用crond服务

[root@linux-node1 ~]# crontab -l
* * * * *  /bin/sh /server/scripts/yum.sh

使用ansible添加一条定时任务

[root@linux-node1 ~]# ansible sunrise -m cron -a "minute=* hour=* day=* month=* weekday=*  job='/bin/sh /server/scripts/test.sh'"

[root@linux-node1 ~]# ansible sunrise -m cron -a "job='/bin/sh /server/scripts/test.sh'"

设置定时任务注释信息,防止重复,name设定

[root@linux-node1 ~]# ansible sunrise -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"

删除相应定时任务

[root@linux-node1 ~]# ansible sunrise -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"

注释相应定时任务,使定时任务失效

[root@linux-node1 scripts]# ansible sunrise -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=no"
minute   分           Minute when the job should run ( 0-59, *, */2, etc )
hour     时           Hour when the job should run ( 0-23, *, */2, etc )
day      日           Day of the month the job should run ( 1-31, *, */2, etc )
month    月           Month of the year the job should run ( 1-12, *, */2, etc )
weekday  周           Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )

job      工作 ;要做的事情(任务,命令)
name     定义定时任务的描述信息,必须写否则无法删除,否则会导致无法删除指定的某条计划任务
disabled 注释定时任务
state
    absent             删除定时任务
    present            创建定时任务(默认为present)

例1:每天的凌晨1点执行rsync_time.sh

1)推送脚本值每台受控端

[root@linux-node1 scripts]# ansible all -m file -a "path=/server/scripts state=directory mode=700"
[root@linux-node1 scripts]# ansible all -m copy -a "src=/server/scripts/rsync_time.sh dest=/server/scripts"

2)创建计划任务

[root@linux-node1 scripts]# ansible all -m cron -a "name='Rsync Time' hour=1 minute=0 job='/bin/sh /server/scripts/rsync_time.sh >/dev/null 2>&1'"

3)删除计划任务

[root@linux-node1 scripts]# ansible all -m cron -a "name='Rsync Time' state=absent"

10 mount模块

仅将挂载的配置写入/etc/fstab,并不会执行挂载操作

[root@linux-node1 ~]# ansible sunrise -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present"

临时挂载设备,并将挂载信息写入/etc/fstab

[root@linux-node1 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"

临时卸载,不会清理/etc/fstab

[root@linux-node1 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted"

卸载,不仅临时卸载,同时会清理/etc/fstab

[root@linux-node1 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent"

参数

path        --- 挂载点
src            --- 需要挂载的设备

fstype        --- 挂载在设备的文件系统
    iso9600 光驱
    ext4
    xfs
    cifs samba的共享文件系统
    ntfs windows磁盘文件系统

opts    --- 挂载属性
    noatime
    noexec
    nosuid

state    --- 挂载动作
    present     --- 开机挂载,仅将挂载配置写入/etc/fstab(不常用)
    mounted     --- 挂载设备,并将配置写入/etc/fstab
    unmounted   --- 卸载设备,不会清除/etc/fstab写入的配置
    absent      --- 卸载设备,会清理/etc/fstab写入的配置

例1:通过nfs实现网络文件共享
1)安装nfs

[root@linux-node1 ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"

2)启动服务

[root@linux-node1 ~]# ansible nfs -m service -a "name=rpcbind state=started enabled=yes"
[root@linux-node1 ~]# ansible nfs -m service -a "name=nfs-utils state=started enabled=yes"

3)修改配置文件
法1:在node01创建配置文件,使用copy模块推给nfs(建议使用)

[root@linux-node1 ~]# mkdir -p /server/conf
[root@linux-node1 ~]#echo '/data 192.168.6.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' > /server/conf/exports
[root@linux-node1 ~]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/exports"

法2:在node01上直接使用copy模块,推送文件内容

[root@linux-node1 ~]# ansible nfs -m copy -a "content='/data 192.168.6.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' dest=/etc/exports"

4)创建目录,用户,并修改所属
通过user group file模块来实现,参考上面的例子

[root@linux-node1 ~]# ansible nfs -m group -a "name=www gid=666"
[root@linux-node1 ~]# ansible nfs -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"
[root@linux-node1 ~]# ansible nfs -m file -a "path=/data owner=666 group=666 recurse=yes state=directory"

5)重载配置文件

[root@linux-node1 ~]# ansible nfs -m service -a "name=nfs state=restarted"

6)在web上挂载nfs的共享目录

[root@linux-node1 ~]# ansible web -m yum -a "name=nfs-utils state=installed"
[root@linux-node1 ~]# ansible web -m shell -a "showmount -e 192.168.6.242"
[root@linux-node1 ~]# ansible web -m mount -a "src=192.168.6.242:/data path=/var/www/html fstype=nfs state=mounted"
文档更新时间: 2019-07-23 09:58   作者:李延召