ansible常用模块

全部模块的文档: https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html

  1. ping模块(可用来测试连通性)
[root@localhost ~]# ansible all -m ping
192.168.0.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  1. setup模块(获取主机信息)

注:在playbook里经常会使用到一个参数gather_facts就与该模块相关,经常会使用参数filter来获取指定信息。例如:

#查看主机的所有信息
[root@localhost ~]# ansible all -m setup

#获取内存信息
[root@localhost ~]# ansible all -m setup -a 'filter=ansible_*_mb'
192.168.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 233, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 317, 
                "used": 165
            }, 
            "real": {
                "free": 233, 
                "total": 482, 
                "used": 249
            }, 
            "swap": {
                "cached": 0, 
                "free": 1023, 
                "total": 1023, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 482, 
        "ansible_swapfree_mb": 1023, 
        "ansible_swaptotal_mb": 1023
    }, 
    "changed": false
}

#获取网卡信息
[root@localhost ~]# ansible all -m setup -a "filter=ansible_eth*"
192.168.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_eth0": {
            "active": true, 
            "device": "eth0", 
            "features": {
                "busy_poll": "off [fixed]", 
                "fcoe_mtu": "off [fixed]", 
                "generic_receive_offload": "on", 
                "generic_segmentation_offload": "on", 
                "highdma": "off [fixed]", 
                "large_receive_offload": "off [fixed]", 
                "loopback": "off [fixed]", 
                "netns_local": "off [fixed]", 
                "ntuple_filters": "off [fixed]", 
                "receive_hashing": "off [fixed]", 
                "rx_all": "off", 
                "rx_checksumming": "off", 
                "rx_fcs": "off", 
                "rx_vlan_filter": "on [fixed]", 
                "rx_vlan_offload": "on", 
                "rx_vlan_stag_filter": "off [fixed]", 
                "rx_vlan_stag_hw_parse": "off [fixed]", 
                "scatter_gather": "on", 
                "tcp_segmentation_offload": "on", 
                "tx_checksum_fcoe_crc": "off [fixed]", 
                "tx_checksum_ip_generic": "on", 
                "tx_checksum_ipv4": "off [fixed]", 
                "tx_checksum_ipv6": "off [fixed]", 
                "tx_checksum_sctp": "off [fixed]", 
                "tx_checksumming": "on", 
                "tx_fcoe_segmentation": "off [fixed]", 
                "tx_gre_segmentation": "off [fixed]", 
                "tx_gso_robust": "off [fixed]", 
                "tx_ipip_segmentation": "off [fixed]", 
                "tx_lockless": "off [fixed]", 
                "tx_mpls_segmentation": "off [fixed]", 
                "tx_nocache_copy": "on", 
                "tx_scatter_gather": "on", 
                "tx_scatter_gather_fraglist": "off [fixed]", 
                "tx_sit_segmentation": "off [fixed]", 
                "tx_tcp6_segmentation": "off [fixed]", 
                "tx_tcp_ecn_segmentation": "off [fixed]", 
                "tx_tcp_segmentation": "on", 
                "tx_udp_tnl_segmentation": "off [fixed]", 
                "tx_vlan_offload": "on [fixed]", 
                "tx_vlan_stag_hw_insert": "off [fixed]", 
                "udp_fragmentation_offload": "off [fixed]", 
                "vlan_challenged": "off [fixed]"
            }, 
            "hw_timestamp_filters": [], 
            "ipv4": {
                "address": "192.168.0.11", 
                "broadcast": "192.168.0.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.0.0"
            }, 
            "ipv6": [
                {
                    "address": "fe80::20c:29ff:fe3d:c87a", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "00:0c:29:3d:c8:7a", 
            "module": "e1000", 
            "mtu": 1500, 
            "pciid": "0000:02:01.0", 
            "promisc": false, 
            "speed": 1000, 
            "timestamping": [
                "tx_software", 
                "rx_software", 
                "software"
            ], 
            "type": "ether"
        }
    }, 
    "changed": false
}

回最上

  1. file模块(文件操作)
  • force: 需要强制创建软链接,一种是源文件不存在的情况,第二种是目标链接已存在,需要取消重新创建。 有两个选项:yes|no
  • group: 定义文件/目录的属组
  • mode: 定义文件/目录的权限
  • owner: 定义文件/目录的属主
  • path: 必选项,定义文件/目录的路径
  • recurse: 递归的设置文件的属性,只对目录有效 有两个选项:yes|no
  • src: 当state=link时,设置源文件的路径
  • dest: 当state=link时,设置目标路径

state参数:

  • directory: 如果目录不存在创建目录
  • file: 如果文件不存在,也不会被创建
  • link: 创建软链接
  • hard: 创建硬链接
  • touch: 如果文件不存在创建文件,当文件已存在时更新修改时间
  • absent: 删除目录,文件或链接
#创建软链接
[root@localhost ~]# ansible all -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"

#创建文件
[root@localhost ~]# ansible all -m file -a "path=/tmp/jeff state=touch"

#删除文件及链接
[root@localhost ~]# ansible all -m file -a "path=/tmp/jeff state=absent"
192.168.0.11 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/jeff", 
    "state": "absent"
}
[root@localhost ~]# ansible all -m file -a "path=/tmp/fstab state=absent"
192.168.0.11 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/fstab", 
    "state": "absent"
}

回最上

  1. copy模块(文件复制)
    复制文件到远程主机
  • backup: 创建一个包含时间戳的原文件,如果源文件被破坏,可以将其恢复原状
  • content: 用于替代“src”, 可以直接设定文件的值
  • dest: 必选项, 将源文件复制到远程主机的绝对路径,如果源文件是个目录,那么该路径也必须是个目录
  • directory_mode: 递归设定目录权限,默认为系统默认权限
  • force: 如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则目标主机的目标位置不存在该文件时,才复制。 默认为yes
  • others: 所有file模块里的选项都可以在这使用
  • src: 要复制到远程主机的文件所在的路径,可以是绝对路径,也可以是相对路径。如果路径是一个目录,将会递归复制。在这种情况下,如果路径以“/”结尾,则只复制目录内的内容,如果没有以“/”结尾,则包含目录在内的整个内容全部复制,类似于rsync。
  • validate: 要在复制到位之前运行的验证命令。要验证的文件的路径是通过’%s’传递的,必须存在,如下例所示。该命令安全地传递,因此扩展和管道等shell功能将无法正常工作。
#复制当前主机的fstab文件到,远端tmp目录下并修改owner为root,mode为644
[root@localhost ~]# ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=root mode=0644"
192.168.0.11 | SUCCESS => {
    "changed": true, 
    "checksum": "c004c449c75f5ea3a2d44f71bb29d5532f6ba618", 
    "dest": "/tmp/fstab", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9486a3c0de25588cabbc2bdebf85f8d9", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 465, 
    "src": "/root/.ansible/tmp/ansible-tmp-1557256735.78-129867274213526/source", 
    "state": "file", 
    "uid": 0
}

#增加backup参数
[root@localhost ~]# ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=root mode=0644 backup=yes"

#将本机/etc/sudoers复制到目标主机,用visudo -cf 检查/etc/sudoers文件是否格式正确
[root@localhost ~]# ansible all -m copy -a "src=/etc/sudoers dest=/etc/sudoers validate=
'visudo -cf %s'"

回最上

  1. service模块
    用于管理服务
  • arguments: 给命令行提供一些选项
  • enabled: 是否开机启动 yes|no
  • name: 必选项,服务名称
  • pattern: 定义一个模式,通过status查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行寻找, 如果匹配到,则认为该服务依然在运行。
  • runlevel: 运行级别
  • sleep: 如果执行了restarted, 在服务stop和start之间沉睡几秒。
  • state: 对当前服务执行启动、停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
#停止nginx
[root@localhost ~]# ansible all -m service -a "name=nginx state=stopped"

#启动nginx
[root@localhost ~]# ansible all -m service -a "name=nginx state=started"

#重启nginx, 等待2秒钟后启动
[root@localhost ~]# ansible all -m service -a "name=nginx state=restarted sleep=2"

#重启网卡
[root@localhost ~]# ansible all -m service -a "name=network state=restarted args=eth0"

回最上

  1. cron模块
    用以管理计划任务
  • backup: 对远程主机上原计划任务修改之前做备份
  • cron_file: 如果有该参数,则用该文件替换远程主机上的cron.d目录下的用户的任务计划,与user模块搭配使用。
  • minute: 分钟(0-59,,/2…) # /2 表示每2分钟
  • hour: 小时(0-23,,/2…)
  • day: 日(1-31,,/2…)
  • month: 月(1-12,,/2…)
  • weekday: 星期几(0-7,*,…)
  • job: 要执行的任务,依赖于state=present
  • name: 该任务的描述
  • special_time: 指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
  • state: 确认该任务计划是创建还是删除,默认创建
  • user: 以哪个用户的身份执行
#需要先将aaa.sh,copy到远程主机的/opt目录下 
#添加reboot时 执行指定脚本
[root@localhost ~]# ansible all -m cron -a "name='running a job when reboot' special_tim
e=reboot job=/opt/aaa.sh"

#因为指定cron_file,且用户为root,所以会保存在/etc/cron.d下,有的test\ ansible文件
#每星期2的12点执行语句,不写默认为*
[root@localhost ~]# ansible all -m cron -a "name='for test' weekday=2 minute=0 hour=12 user=root job='cat /etc/passwd > /root/111' cron_file='test ansible'"

#清空指定cronfile
ansible all -m cron -a "name='for test' cron_file='test ansible' state=absent"

回最上

  1. yum模块
    使用yum包管理器来管理软件包
  • config_file: yum的配置文件
  • disable_gpg_check: 关闭gpg_check
  • disablerepo: 不启用某个源
  • enablerepo: 启用某个源
  • name: 要进行操作的软件包名,也可以传递一个url 或者 本地rpm包路径
  • state: 状态(present,absent,latest)
#安装最新ntp
[root@localhost ~]# ansible all -m yum -a "name=ntp state=latest"

#下载指定版本、系统的rpm包(nginx为例)
[root@localhost ~]# ansible all -m yum -a "name=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm state=present"

#再安装nginx
[root@localhost ~]# ansible all -m yum -a "name=nginx state=latest"

#卸载服务
[root@localhost ~]# ansible all -m yum -a "name=nginx state=absent"

回最上

  1. get_url模块
    该模块主要用于从http、ftp、https服务器上下载文件(类似于wget)
  • sha256sum: 下载完成后进行sha256check
  • timeout: 下载超时时间,默认10s
  • url: 下载的url
  • url_password、url_username: 用于需要用户名和密码验证的情况
  • use_proxy: 使用代理,默认为no。 需要在远程主机的环境变量中定义
#下载指定rpm包到tmp目录下 权限为0644
[root@localhost ~]# ansible all -m get_url -a "url=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm dest=/tmp/ mode=0644"
  1. synchronize模块
    Ansible synchronize 模块主要用于目录、文件同步,基于 Rsync 命令同步目录
  • compress 开启压缩,默认为开启;

  • archive 是否采用归档模式同步,保证源和目标文件属性一致;

  • checksum 是否效验;

  • dirs 以非递归的方式传输目录;

  • links 同步链接文件;

  • recursive 是否递归 yes/no;

  • rsync_opts 使用 rsync 的参数;

  • copy_links 同步的时候是否复制连接;

  • delete 删除源中没有而目标存在的文件;

  • src 源目录及文件;

  • dest 目标目录及文件;

  • dest_port 目标接受的端口;

  • rsync_path 服务的路径,指定 rsync 命令来在远程服务器上运行;

  • rsync_timeout 指定 rsync 操作的 IP 超时时间;

  • set_remote_user 设置远程用户名;

  • –exclude=.log 忽略同步.log 结尾的文件;

  • mode 同步的模式,rsync 同步的方式 PUSH、PULL,默认都是推送 push。

示例:

  1. 基于synchronize模块,将源目录(ansible本机)同步至目标目录(172.25.70.2)(增量同步)
ansible 172.25.70.2 -m synchronize -a "src=/tmp/ dest=/tmp/"
  1. 基于synchronize模块,将源目录(ansible本机)同步至目标目录(172.25.70.2)(完全同步)
ansible 172.25.70.2 -m synchronize -a "src=/tmp/ dest=/tmp/ delete=yes"
  1. 相当于rsync
# a表示archive  -P表示显示进度  --delete完全同步删除多余文件、目录
rsync -aP --delete /tmp/ root@172.25.70.2:/tmp/

回最上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值