- A+
Ansible传送门
Ansible常用模块
我们前文说过,Ansible是高度模块化的,内置模块很多,这里整理Ansible常用模块。
command -a 'COMMAND' user -a 'name= state={present|absent} system= uid=' group -a 'name= gid= state= system=' cron -a 'name= minute= hour= day= month= weekday= job= user= state=' copy -a 'dest= src= mode= owner= group=' file -a 'path= mode= owner= group= state={directory|link|present|absent} src=' ping 没有参数 yum -a 'name= state={present|latest|absent}' service -a 'name= state={started|stopped|restarted} enabled=' shell -a 'COMMAND' script -a '/path/to/script' setup
user模块
用户模块,管理用户帐号
action: user
comment # 用户的描述信息
createhome # 是否创建家目录
force # 在使用state=absent是, 行为与userdel –force一致.
group # 指定基本组
groups # 指定附加组,如果指定为(groups=)表示删除所有组
home # 指定用户家目录
login_class # 可以设置用户的登录类 FreeBSD, OpenBSD and NetBSD系统.
move_home # 如果设置为home=时, 试图将用户主目录移动到指定的目录
name # 指定用户名
non_unique # 该选项允许改变非唯一的用户ID值
password # 指定用户密码
remove # 在使用state=absent时, 行为是与userdel –remove一致
shell # 指定默认shell
state # 设置帐号状态,不指定为创建,指定值为absent表示删除
system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid # 指定用户的uid
update_password # 更新用户密码
示例
创建用户tom,用户信息为tom is
tom,uid为1066,基本组为tom,附加组为wheel,shell类型为zshell,用户家目录为/home/tomhome
[root@Ansible ~]#ansible web -m user -a 'name=tom comment="tom is tom" uid=1066 group=tom groups=wheel shell=/bin/zshell home=/home/tomhome' [root@Ansible ~]#getent passwd tom tom:x:1066:1002:tom is tom:/home/tomhome:/bin/zshell
group模块
用户组模块,添加或删除组
action: group
gid # 设置组的GID号
name= # 管理组的名称
state # 指定组状态,默认为创建,设置值为absent为删除
system # 设置值为yes,表示为创建系统组
cron模块
定时任务模块,设置管理节点生成定时任务
action: cron
backup # 如果设置,创建一个crontab备份
cron_file # 如果指定, 使用这个文件cron.d,而不是单个用户crontab
day # 日应该运行的工作( 1-31, *, */2, etc )
hour # 小时( 0-23, *, */2, etc )
job # 指明运行的命令是什么
minute # 分钟( 0-59, *, */2, etc )
month # 月( 1-12, *, */2, etc )
name # 定时任务描述
reboot # 任务在重启时运行,不建议使用,建议使用special_time
special_time #
特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state # 指定状态,prsent表示添加定时任务,也是默认设置,absent表示删除定时任务
user # 以哪个用户的身份执行
weekday # 周( 0-6 for Sunday-Saturday, *, etc )
示例:所有主机添加ntp定时任务
[root@py ~]# ansible all -m cron -a 'name="sync time from netserver" minute="*/10" job="/usr/sbin/ntpdate 10.0.0.1 &> /dev/null"' 192.168.1.3 | SUCCESS => { "changed": true, "envs": [], "failed": false, "jobs": [ "sync time from netserver" ] } [root@py ~]# crontab -l #Ansible: sync time from netserver */10 * * * * /usr/sbin/ntpdate 10.0.0.1 &> /dev/null
示例:所有主机删除ntp定时任务
删除只需要指定name
[root@py ~]# ansible all -m cron -a 'name="sync time from netserver" state=absent' 192.168.1.3 | SUCCESS => { "changed": true, "envs": [], "failed": false, "jobs": [] } [root@py ~]# crontab -l */5 * * * * /usr/sbin/ntpdate time.windows.com; /usr/sbin/hwclock -w > /dev/null 2>&
copy模块
复制模块,将文件复制到被管理主机
action: copy
backup # 创建一个备份文件包括时间戳信息,如果以某种方式重创错了,还可以拿回原始文件
content # 取代src=,表示直接用此处指定的信息生成为目标文件内容
dest # 远程节点存放文件的路径,必须是绝对路径
directory_mode # 递归复制设置目录权限,默认为系统默认权限
force #
如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果设置为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
group # 复制到远程主机后,指定文件或目录的属组
mode # 复制到远程主机后,指定文件或目录权限,类似与chmod指明如 0644
owner # 复制到远程主机后,指定文件或目录属主
src #
要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用”/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
示例:
将本地的/etc/fatab文件复制到webserver主机组的/tmp/ansible.log,属主为root,属组为root,权限为600
ansible webserver -m copy -a 'src=/etc/fstab dest=/tmp/fstab.tmp owner=root group=root mode=600'
file模块
文件操作模块,设置文件属性
action: file
force #
需要在两种情况下强制创建软连接,一种是源文件不存在但之后会建立的情况下;另一种是目标连接已存在,需要先取消之前的软连接,有两个选项:yes|no
group # 设置文件或目录的属组
mode # 设置文件或目录的权限
owner # 设置文件或目录的属主
path # 必选项,定义文件或目录的路径
recurse # 递归设置文件的属性,只对目录有效
src # 要被链接到的路径,只应用与state=link的情况
state # directory:如果目录不存在,创建目录
示例1:
所有主机组/tmp目录下创建testdir目录
ansible all -m file -a 'path=/tmp/testdir state=directory'
示例2:
webserrver主机组创建test.txt文件,权限600,属主属组为root
ansible webserver -m file -a 'path=/tmp/test.txt state=touch mode="600" owner=root group=root'
示例3:
所有主机组创建test_link软连接文件,原文件是test.txt
ansible all -m file -a 'path=/tmp/test_link state=link src=/tmp/test.txt'
示例4:
所有主机组强制删除建test_link软连接文件
[root@py ~]# ansible all -m file -a 'path=/tmp/test_link state=absent force=yes'
ping模块
检测主机存活,没有参数
所有主机执行ping存活检测
[root@py ~]# ansible all -m ping 192.168.1.3 | SUCCESS => { "changed": false, "failed": false, "ping": "pong" }
hostname模块
设置系统的主机名模块
示例:
将172.16.250.149主机名改为master
[root@Ansible ~]#ansible 172.16.250.149 -m hostname -a 'name=master' [root@Ansible ~]#hostname master
yum 模块
基于yum源安装程序
action: yum
conf_file # yum的配置文件
disable_gpg_check # 关闭gpg_check
disablerepo # 不启用某个源
enablerepo # 启用某个源
name= # 指定要安装的包,如果有多个版本需要指定版本,否则安装最新的包
state # 安装(present),安装最新版(latest),卸载程序包(absent)
示例:
为web组所有主机安装nginx 且为最新版本
[root@Ansible ~]#ansible web -m yum -a 'name=nginx state=latest' service
service模块
服务管理模块
action: service
arguments # 向服务传递的命令行参数
enabled # 设置服务开机自动启动,参数为yes|no
name # 控制服务的名称
pattern #
定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行
runlevel # 设置服务自启动级别
sleep # 如果执行了restarted,则在stop和start之间沉睡几秒钟
state # 启动started 关闭stopped 重新启动restarted 重载reloaded
示例1:
web组所有主机启动nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=started'
示例2:
web组所有主机关闭nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=stopped'
示例3:
web组所有主机重启nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=restarted'
示例4:
web组所有主机重载nginx配置文件
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=reloaded'
示例5:
web组所有主机启动nginx,并开机启动/不启动
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=started enabled=yes/no'
script模块
在指定节点运行服务端的脚本
[root@Ansible ~]#vim test.sh #/bin/bash touch /tmp/test.sh.log echo "hello" >> /tmp/test.sh.log
示例1:
在web组中所有主机执行/root/test.sh脚本
[root@Ansible ~]#ansible web -m script -a '/root/test.sh' [root@Ansible ~]#cat /tmp/test.sh.log hello
示例2:
查看172.16.251.163主机下的/tmp/test.sh.log
[root@Ansible ~]#ansible 172.16.251.163 -m shell -a ‘cat /tmp/test.sh.log’ 172.16.251.163 | SUCCESS | rc=0 >> hello