Ansible指令和常用模块使用

这里文章记录一下ansible的指令选项和常用的模块使用

ansible指令选项

  • -m:要执行的模块,默认为command
  • -a:模块的参数
  • -u:ssh连接的用户名,默认用root,ansible.cfg中可以配置
  • -k:提示输入ssh登录密码,当使用密码验证的时候用
  • -s:sudo运行
  • -U:sudo到哪个用户,默认为root
  • -K:提示输入sudo密码,当不是NOPASSWD模式时使用
  • -C:只是测试一下会改变什么内容,不会真正去执行
  • -c:连接类型(default=smart)
  • -f:fork多少进程并发处理,默认为5个
  • -i:指定hosts文件路径,默认default=/etc/ansible/hosts
  • -I:指定pattern,对已匹配的主机中再过滤一次
  • --list-host:只打印有哪些主机会执行这个命令,不会实际执行
  • -M:要执行的模块路径,默认为/usr/share/ansible
  • -o:压缩输出,摘要输出
  • --private-key:私钥路径
  • -T:ssh连接超时时间,默认是10秒
  • -t:日志输出到该目录,日志文件名以主机命名
  • -v:显示详细日志

ansible指令使用方法:

ansible可以直接在命令中指定主机组或者主机来着执行命令:例如

[root@Ansible ~]# ansible nginx -m ping
192.168.214.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
...省略...
[root@Ansible ~]# ansible 192.168.214.131 -m ping
192.168.214.131 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

同时更ansible更灵活的是可以使用通配符来匹配符合规则的主机或主机组来执行命令

通配符匹配

匹配所有主机:all 或者 *

[root@Ansible ~]# ansible all  -m ping
192.168.214.132 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
...省略...
或
[root@Ansible ~]# ansible 192.168.214.*  -m ping
192.168.214.131 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
...省略...

匹配主机组中部分主机: 用 :隔开主机名

[root@Ansible ~]# ansible 192.168.214.128:192.168.214.132  -m shell -a "hostname"
192.168.214.132 | SUCCESS | rc=0 >>
nginx-2

192.168.214.128 | SUCCESS | rc=0 >>
localhost.localdomain

选择主机组中的部分主机执行:比如第1台到第3台 [0:2]按段执行

[root@Ansible ~]# ansible nginx[0:2] -m shell -a "hostname"
192.168.214.131 | SUCCESS | rc=0 >>
nginx-1

192.168.214.128 | SUCCESS | rc=0 >>
localhost.localdomain

192.168.214.129 | SUCCESS | rc=0 >>
file-server

执行多个主机组

[root@Ansible ~]# ansible nginx:centos7 -m shell -a "hostname"
192.168.214.132 | SUCCESS | rc=0 >>
nginx-2

192.168.214.129 | SUCCESS | rc=0 >>
file-server

192.168.214.128 | SUCCESS | rc=0 >>
localhost.localdomain

192.168.214.131 | SUCCESS | rc=0 >>
nginx-1

ansible常用模块

ansible模块分核心模块和额外模块,其中核心模块按照模块功能来划分,分别为:云模块、命令模块、数据库模块、文件模块、资产模块、消息模块、监控模块、网络模块、通知模块、包管理模块、源码控制模块、系统模块、单元模块、web设施模块、windows模块等。
下面记录一些常用的模块:
使用指令查看ansible支持哪些模块:ansible-doc -l
查看某个模块有哪些参数: ansible-doc [模块名]

shell模块

默认ansible使用的module 是 command,这个模块并不支持 shell 变量和管道等,若想使用shell 来执行模块,请使用-m 参数指定 shell 模块
使用指令的模块后面要加‘-a’参数指定模块参数

[root@Ansible ~]# ansible centos7 -m shell -a "cat /etc/passwd | grep ^root.*"
192.168.214.128 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
file模块

file模块主要用于远程主机上的文件操作
模块参数

force        #需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no;
group        #定义文件/目录的属组;
mode         #定义文件/目录的权限;
owner        #定义文件/目录的属主;
path         #必选项,定义文件/目录的路径, required;
recurse      #递归的设置文件的属性,只对目录有效;
src          #要被链接的源文件的路径,只应用于state=link的情况;
dest         #被链接到的路径,只应用于state=link的情况;
state:
directory  #如果目录不存在,创建目录;
file       #即使文件不存在,也不会被创建;
link       #创建软链接;
hard       #创建硬链接;
touch      #如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间;
absent     #删除目录、文件或者取消链接文件;

用法示例
在远程主机/tmp/下创建一个a.txt

扫描二维码关注公众号,回复: 113953 查看本文章
[root@Ansible ~]# ansible centos7 -m file -a "dest=/tmp/a.txt state=touch"

将文件删除

[root@Ansible ~]# ansible centos7 -m file -a "dest=/tmp/a.txt state=absent"

将/tmp/b.txt属组属主该成yufu

[root@Ansible ~]# ansible centos7 -m file -a 'dest=/tmp/b.txt owner=yufu group=yufu'
copy模块

copy模块是复制文件到远程主机

模块参数

src #原文件路径
dest #目标路径
force #是否强制覆盖,yes或no

将本机的c.txt 复制到远程主机

[root@Ansible ~]# ansible centos7 -m copy -a 'src=/tmp/c.txt dest=/tmp/'
192.168.214.128 | SUCCESS => {
service模块

service模块用来管理远程主机的服务,这里不分centos6和7的管理方式,都用参数指定

模块参数

name     # 指定服务名称
enabled     #是否要加入开机启动
state      #运行状态,状态名称要用英文过去式

用法示例
将远程httpd服务启动

[root@Ansible ~]# ansible centos7 -m service -a "name=httpd state=started"
192.168.214.128 | SUCCESS => {

关闭httpd

[root@Ansible ~]# ansible centos7 -m service -a "name=httpd state=stopped"
192.168.214.128 | SUCCESS => {
cron模块

cron模块用于管理任务计划

模块参数

name #对执行任务的描述
backup “Ture | Flase” #执行前备份
cron_file  #使用本地的cron文件覆盖远程主机的cron 文件
day  # 每天 (1-31, * ,*/2,)
hour  #小时 ( 0-23, * ,*/2,)
minute  #分钟 (0-59, * , */2,)
month   #月  ( 0-12, * ,*/2,)
weekday   #周 (0-6, * ,)
user  #以哪个用户执行
state=absent|present #确认任务计划执行还是删除
job  #要执行的任务

用法示例

[root@Ansible ~]# ansible centos7 -m cron -a "name='test' minute='*/1' user='root' job='date'"
查看
[root@localhost ~]# crontab -l
#Ansible: test
*/1 * * * * date
yum模块

yum模块用来管理安装软件包

模块部分参数

list  #列出软件包
naem   #包名称
state   #确定对包的操作:present or installed,or remove absent or removed

应用示例

[root@Ansible ~]# ansible centos7 -m yum -a "name=http state=removed"
192.168.214.128 | SUCCESS => {
user模块

user模块用来管理用户和组

模块参数

home        #指定用户的家目录,需要与createhome配合使用;
groups      #指定用户的属组;
uid         #指定用的uid;
password    #指定用户的密码;
name        #指定用户名;
createhome  #是否创建家目录yes|no;
system      #是否为系统用户;
remove      #当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r;
state       #是创建还是删除;
shell       #指定用户的shell环境;

用法示例

不删家目录
[root@Ansible ~]# ansible centos7 -m user -a "name=yufu state=absent"
192.168.214.128 | SUCCESS => {

删除家目录

[root@Ansible ~]# ansible centos7 -m user -a "name=feng state=absent remove=yes" 
192.168.214.128 | SUCCESS => {
mount模块

mount模块用来挂载

模块参数

fstype    #必选项,挂载文件的类型;
name      #必选项,挂载点;
opts      #传递给mount命令的参数;
src       #必选项,要挂载的文件;
state     #必选项;
present   #只处理fstab中的配置;
absent    #删除挂载点;
mounted   #自动创建挂载点并挂载之;
umounted  #卸载;

应用示例

[root@Ansible ~]# ansible centos7 -m mount -a"src=/dev/sda4 name=/mnt fstype=ext4 opts=rw state=mounted"
192.168.214.128 | SUCCESS => {
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "ext4", 
    "name": "/mnt", 
    "opts": "rw", 
    "passno": "0", 
    "src": "/dev/sda4"
}

有关模块内容暂时就写这么多吧,如果以后有用其他模块再来补充。

猜你喜欢

转载自www.cnblogs.com/anay/p/8996176.html