Ansible 常用模块

  根据zs官方的分类,将模块按功能分类为:云模块、命令模块、数据库模块、文件模块、资产模块、消息模块、监控模块、网络模块、通知模块、包管理模块、源码控制模块、系统模块、单元模块、web设施模块、windows模块 ,具体可以参看官方页面

一、ping 模块

  测试主机是否在线,在线返回pong

[root@localhost ~]# ansible t3 -m ping 
192.168.11.162 | SUCCESS => {
    "changed": false,
    "failed": false,
    "ping": "pong"
}

二、setup 模块

  主要用于获取主机信息 :架构信息,IP,时间,域名,网卡,MAC,主机名,CPU等信息。在playbooks里经常会用到的一个参数gather_facts就与该模块相关。setup模块下经常使用的一个参数是filter参数

查看内存信息
[root@localhost ~]# ansible t3 -m setup -a "filter=ansible_*_mb"
192.168.11.162 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 206,
        "ansible_memory_mb": {
            "nocache": {
                "free": 1224,
                "used": 615
            },
            "real": {
                "free": 206,
                "total": 1839,
                "used": 1633
            },
            "swap": {
                "cached": 0,
                "free": 4095,
                "total": 4095,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 1839,
        "ansible_swapfree_mb": 4095,
        "ansible_swaptotal_mb": 4095
    },
    "changed": false,
    "failed": false
}

查看内存信息并存到本地/tmp/facts/目录

[root@localhost ~]# ansible t3 -m setup -a "filter=ansible_*_mb" --tree /tmp/facts
[root@localhost ~]# cat /tmp/facts/192.168.11.162 
{"ansible_facts": {
    "ansible_memfree_mb": 206,
    "ansible_memory_mb": {
        "nocache": {
            "free": 1224,
            "used": 615
        },
        "real": {
            "free": 206,
            "total": 1839,
            "used": 1633
        },
        "swap": {
            "cached": 0,
            "free": 4095,
            "total": 4095,
            "used": 0}
    },
    "ansible_memtotal_mb": 1839,
    "ansible_swapfree_mb": 4095,
    "ansible_swaptotal_mb": 4095
    },
"changed": false,
"failed": false
}

三、synchronize 模块

     使用rsync同步文件

   参数:
  archive: 归档,相当于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启
  checksum: 跳过检测sum值,默认关闭
  compress:是否开启压缩
  copy_links:复制链接文件,默认为no ,注意后面还有一个links参数
   delete: 删除不存在的文件,默认no
  src: 源路径
  dest:目的路径
  dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议
  dirs:传速目录不进行递归,默认为no,即进行目录递归
  rsync_opts:rsync参数部分
  set_remote_user:主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不同的情况
  mode: push或pull 模块,push模的话,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件
ansible t3  -m synchronize -a   "src=/tmp/nginx-1.13.3 dest=/tmp/ 
rsync_opts='--exclude=configure,--exclude=LICENSE'"
ansible t3  -m synchronize -a   "src=/tmp/nginx-1.13.3 dest=/tmp/  checksum=yes 
times=no links=yes  rsync_opts='--exclude=configure,--exclude=LICENSE'"

四、command 模块和shell

  运行指定的命令

  shell 和command 区别:shell模块可以使用特殊字符,command 是不支持的

[root@localhost ~]# ansible t3 -m command -a 'date'
192.168.11.162 | SUCCESS | rc=0 >>
2017年 12月 26日 星期二 15:54:22 CST

[root@localhost ~]# ansible t3 -m command -a 'mkdir /tmp/test'
 [WARNING]: Consider using file module with state=directory rather than running mkdir

192.168.11.162 | SUCCESS | rc=0 >>

[root@localhost ~]# ansible t3 -m shell -a 'rm -rf /tmp/test'
 [WARNING]: Consider using file module with state=absent rather than running rm

192.168.11.162 | SUCCESS | rc=0 >>

 五、script 模块 远程主机执行本地脚步

[root@localhost tmp]# ansible t3 -m script -a "/tmp/a.sh"
192.168.11.162 | SUCCESS => {
    "changed": true,
    "failed": false,
    "rc": 0,
    "stderr": "Shared connection to 192.168.11.162 closed.\r\n",
    "stdout": "Hello\r\n",
    "stdout_lines": [
        "Hello"
    ]
}

 六、user模块:管理用户的模块

  模块参数详解:
    name:指定用户名
    password:设定用户密码,password参数需要接受md5加密后的值
    state:用户状态,默认为present
      present:表示添加用户
      absent:表示删除用户
    update_password:修改用户密码
      always:新密码和旧密码不同时进行修改
      on_create:为新创建的用户指定密码
    createhome:创建家目录
      yes:默认项,即创建用户默认是有家目录的
      no:创建用户时不创建家目录
    remove:
      yes:删除用户家目录,需要指定此参数
      no:默认项,删除用户时默认不删除用户的家目录
    system:
      yes:默认创建为普通用户,而非系统用户
    如果不指定默认生成的选项有:
      home:创建家目录
      shell:创建默认的shell为/bin/bash
      system:默认创建为普通用户,而非系统用户,指定是用yes

[root@localhost tmp]# ansible-doc -s user              #查看帮助信息
user模块中的password是需要经过md5加密的
[root@localhost ~]# echo 123456 |openssl passwd -1 -stdin
$1$XIaOAwgN$up3m2U5i6gIBl87P7iCh60
#添加一个用户
[root@localhost ~]# ansible t3 -m user -a 'name=simon system=yes \
password=$1$XIaOAwgN$up3m2U5i6gIBl87P7iCh60 state=present'
192.168.11.162 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "failed": false,
    "group": 992,
    "home": "/home/simon",
    "name": "simon",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 995
}

#删除一个用户
[root@localhost ~]# ansible t3 -m user -a 'name=simon remove=yes state=absent'
192.168.11.162 | SUCCESS => {
    "changed": true,
    "failed": false,
    "force": false,
    "name": "simon",
    "remove": true,
    "state": "absent",
    "stderr": "userdel: simon 邮件池 (/var/spool/mail/simon) 未找到\n",
    "stderr_lines": [
        "userdel: simon 邮件池 (/var/spool/mail/simon) 未找到"
    ]
}
#更新密码
[root@localhost ~]# echo 654321 | openssl passwd -1 \
-stdin $1$4IFTCb3k$Oq3i9sKTxU/IZ5HKAVCuO/
[root@localhost ~]# ansible t3 -m user -a 'name=simon 
update_password=always password=$1$4IFTCb3k$Oq3i9sKTxU/IZ5HKAVCuO/'
192.168.11.162 | SUCCESS => {
    "append": false,
    "changed": true,
    "comment": "",
    "failed": false,
    "group": 992,
    "home": "/home/simon",
    "move_home": false,
    "name": "simon",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 995
}

 

 七、yum模块:任务计划模块

  使用yum包管理器来管理软件包,其选项有:
    config_file:yum的配置文件
    disable_gpg_check:关闭gpg_check
    disablerepo:不启用某个源
    enablerepo:启用某个源
    name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
    state:状态(present,absent,latest)

      present:默认的,表示为安装

      lastest:安装为最新的版本

                    absent:表示删除

 

#安装最新版本的apache
	[root@localhost ~]# ansible test -m yum -a 'name=httpd state=latest'
	#移除apache
	[root@localhost ~]# ansible test -m yum -a 'name=httpd state=absent'
	#安装指定版本
	[root@localhost ~]# ansible test -m yum -a 'name=httpd-2.2.29-1.4.amzn1 state=present'
	
	#安装整个Development tools相关的软件包
    [root@localhost ~]# ansible test -m yum -a 'name="@Development tools" state=present'
	#从一个远程yum仓库安装nginx
    [root@localhost ~]# ansible test -m yum -a 'name=\
http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm \
state=present'

    

 八、cron模块:任务计划模块

  获取帮助:ansibe-doc -s cron
  模块参数详解:
  state:
    present:创建任务
    absent:删除任务
  backup:对远程主机上的原任务计划内容修改之前做备份
  job:要执行的任务
  name:该任务的描述(必须项)
  user:以哪个用户的身份运行
  minute:分钟(0-59,*,*/2,……),默认为*
  hour:小时(0-23,*,*/2,……),默认为*
  day:日(1-31,*,*/2,……),默认为*
  month:月(1-12,*,*/2,……),默认为*
  weekday:周(0-7,*,……),默认为*

  

[root@docker1 ~]# ansible t3 -m cron -a 'name="sync time from ntpserver" minute=*/10 
job="/usr/sbin/ntpdate 3.cn.pool.ntp.org"'
192.168.11.162 | SUCCESS => {
    "changed": false,
    "envs": [],
    "failed": false,
    "jobs": [
        "sync time from ntpserver"
    ]
}

 

 九、service模块:管理服务

管理服务

options:

    arguments: 给命令行提供一些选项,aliases: args

    enabled:是否开机启动 yes|no

    name: 必须,服务名称

    pattern:定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行。

    runlevel:运行级别

    sleep:如果执行了restarted,在则stop和start之间沉睡几秒钟

    state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

    use:服务模块实际上使用系统特定的模块,通常是自动检测,这个设置可以强制一个特定的模块。通常情况下,使用ansible_service_mgr 的值,并在找不到匹配的情况下推到旧的服务模块

# 启动nginx服务,没有运行则启动
[root@docker1 ~]# ansible t3 -m service -a "name=nginx state=started enabled=yes"
# 停止nginx服务
[root@docker1 ~]# ansible t3 -m service -a "name=nginx state=stopped"
# 重启nginx 服务
[root@docker1 ~]# ansible t3 -m service -a "name=nginx state=restarted"
# 重新加载
[root@docker1 ~]# ansible t3 -m service -a "name=nginx state=reloaded"
# 启动服务foo
[root@docker1 ~]# ansible t3 -m service -a "name=foo pattern=/usr/bin/foo state=started"
# 重启eth0 网卡
[root@docker1 ~]# ansible t3 -m service -a "name=network  state=restarted args=eth0"

 

 十、unarchive模块:用于解压文件

 options:

    copy: 在解压文件之前,是否先将文件复制到远程主机,默认为yes,no 则要求目标主机上压缩包必须存在。

    creates:指定一个文件名,当改文件存在时,则解压指令不执行。

    dest: 远程主机上的一个路径,即文件解压的路径

    grop:解压后的目录或文件的属组

    list_files: yes,则会列出压缩包里的文件,默认为no

    mode: 解压后文件的权限

    src:如果copy为yes ,则需要指定压缩文件的源路径。

    owner: 解压后文件或目录的属主

 

- name: Extract foo.tgz into /var/lib/foo
  unarchive:
    src: foo.tgz
    dest: /var/lib/foo

- name: Unarchive a file that is already on the remote machine
  unarchive:
    src: /tmp/foo.zip
    dest: /usr/local/bin
    remote_src: yes

- name: Unarchive a file that needs to be downloaded (added in 2.0)
  unarchive:
    src: https://example.com/example.zip
    dest: /usr/local/bin
    remote_src: yes

转载于:https://www.cnblogs.com/lmx1002/p/8087014.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值