Ansible 常用模块介绍

Ansible 常用模块介绍

简介:模块也称为task,是在ansible中时间在执行的。

ping 模块

检查指定节点机器是否能连通,用法很简单,不涉及参数。如果被检测的主机在线,则返回pong

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

 

setup模块

setup模块用于收集远程主机的一些基本信息。

常用参数:

filter :用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

[root@localhost ~]# ansible 192.168.137.102 -m setup
# 不加选项会返回所有的信息。信息太多了 这里就不贴出来了。

#获取ip地址
[root@localhost ~]# ansible 192.168.137.102 -m setup -a "filter=ansible_all_ipv4_addresses"
192.168.137.102 | SUCCESS => {
   "ansible_facts": {
       "ansible_all_ipv4_addresses": [
           "192.168.137.102"
      ]
  },
   "changed": false
}

#获取是什么系统
[root@localhost ~]# ansible 192.168.137.102 -m setup -a "filter=ansible_distribution"
192.168.137.102 | SUCCESS => {
   "ansible_facts": {
       "ansible_distribution": "CentOS"
  },
   "changed": false
}

#其他常用的信息。
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。

 

command 模块

command 模块可以帮助我们在远程主机上执行命令,使用的时候可以不用 加 -m 指定。command 是ansible 默认使用的模块。 (可以在配置文件中修改默认模块)

# default module name for /usr/bin/ansible
#module_name = command

注意:使用command在远程主机执行命令的时候,不会经过shell处理。如果命令带有重定向,管道符等会失效。

[root@localhost ~]# ansible 192.168.137.102 -a 'uptime'
192.168.137.102 | SUCCESS | rc=0 >>
15:44:41 up  1:33,  2 users, load average: 0.00, 0.01, 0.05

[root@localhost ~]# ansible 192.168.137.102 -a 'ls /root/'
192.168.137.102 | SUCCESS | rc=0 >>
anaconda-ks.cfg

 

shell 模块

shell 模块可以帮助我们在远程主机上执行命令。与 command 模块不同的是,shell 模块在远程主机中执行命令时,会经过远程主机上的 /bin/sh 程序处理。

与command模块使用方法类似,只不过支持管道,重定向,变量符等等。由于command比较安全有可预知性,所以我们平时用的时候最好用command。command无法满足需求时,在使用shell。

[root@localhost ~]# ansible 192.168.137.102 -m shell -a 'netstat -lnpt|grep 3306'
192.168.137.102 | SUCCESS | rc=0 >>
tcp6       0      0 :::3306                 :::*                   LISTEN      1410/mysqld  

远程执行脚本:

 首先创建一个shell脚本
vim /tmp/test.sh //加入内容
#!/bin/bash
echo `date` > /tmp/ansible_test.txt
然后把该脚本分发到各个机器上
ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
最后是批量执行该shell脚本
ansible testhost -m shell -a "/tmp/test.sh"

 

file 模块

file 模块可以帮助我们完成一些对文件的基本操作。比如,创建文件或目录、删除文件或目录、修改文件权限等。

包含如下选项:

force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no

group:定义文件/目录的属组

mode:定义文件/目录的权限

owner:定义文件/目录的属主

path:必选项,定义文件/目录的路径

recurse:递归设置文件的属性,只对目录有效

src:被链接的源文件路径,只应用于state=link的情况

dest:被链接到的路径,只应用于state=link的情况

state:

  =directory:如果目录不存在,就创建目录

 =file:即使文件不存在,也不会被创建

 = link:创建软链接

 =hard:创建硬链接

 =touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间

  =absent:删除目录、文件或者取消链接文件

#创建一个目录,如果目录存在,则不做任何操作。
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/ state=directory"

#创建一个文件,如果文件存在,则更新文件时间,与touch命令相同
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/testfile state=touch"

#创建软链接 path=是新目标路径,src 是源路径
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_link state=link src=/root/test/testfile"

#创建硬链接
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_hard state=hard src=/root/test/testfile"

#创建软链接时,如果重名 会强制覆盖。
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_link state=link src=/root/test/testfile force=yes"

#删除远程文件或者是目录
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_hard state=absent"

#创建文件时,指定属主或者修改属主
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_1 state=touch user=mysql"
#创建文件时,指定属主或者修改属组
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_1 state=touch group=mysql"

#创建文件或目录时,指定权限 或者修改权限
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/test_2 state=touch mode=0644"

#递归修改属主
[root@localhost ~]# ansible 192.168.137.102 -m file -a "path=/root/test/ state=directory owner=mysql group=mysql recurse=yes"

 

copy 模块

功能:实现主控端向目标主机copy文件。

参数:

#src    主控端文件位置
#dest 要将源文件复制到远程机器的绝对路径,必选项。
#owner 文件复制过去后的所有者
#group 文件复制过去后的所属组
#mode   文件的权限设定,执行a+x这种方式
#backup 在覆盖之前将源文件备份,备份文件包含时间信息。 如果与远程主机下的文件不一致,才会备份
#directory_mode:递归设定目录的权限。

 

拷贝文件:

ansible 192.168.137.102 -m copy -a 'src=/root/frs/hosts dest=/root/ '

拷贝文件,设置权限

[root@localhost ~]# ansible 192.168.137.102 -m copy -a 'src=/root/frs/hosts dest=/root/ mode=600'

拷贝目录:

[root@localhost ~]# ls -l
总用量 766496
-rw-------. 1 root root      1616 10月  9 01:05 anaconda-ks.cfg
drwxr-xr-x. 2 root root        32 12月 19 15:43 ansible
drwxr-xr-x. 3 root root        70 12月 19 15:20 frs
-rw-r--r--. 1 root root 784882878 12月 18 14:13 frs.zip
drwxrwxr-x. 3 root root        17 12月 14 09:23 __MACOSX
-rw-r--r--. 1 root root      2139 12月 18 14:03 操作步骤.txt

[root@localhost ~]# ansible 192.168.137.102 -m copy -a 'src=/root/frs dest=/root/ '
192.168.137.102 | SUCCESS => {
   "changed": true,
   "dest": "/root/",
   "src": "/root/frs"
}

 

service 模块

用于远程机的服务管理。

该模块包含如下选项:​

arguments:给命令提供一些选项

enabled:是否开机启动 yes|no

name:必须选项,服务名称

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

runlevel:运行级别

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

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

例子:

#关闭远程机iptables
[root@ansible ~]# ansible zyos.com -m service -a "name=iptables state=stopped"

#关闭mysqld
[root@ansible ~]# ansible zy.com -m service -a "name=mysqld state=started"
[root@ansible ~]# ansible zy.com -m service -a "name=mysqld state=restarted sleep=20"

 

cron 计划任务模块

Ansible cron模块主要用于添加、删除、更新操作系统的crontab任务计划

cron模块使用详解:

- name:任务计划名称
- cron_file:替换客户端该用户的任务计划的文件
- minute:分(0-59, * ,*/2)
- hour:时(0-23, * ,*/2)
- day:日(1-31, * ,*/2)
- month:月(1-12, * , */2)
- weekday:周(0-6或1-7, *)
- job:任何计划执行的命令,state要等于present
- backup:是否备份之前的任务计划
- user:新建任务计划的用户
- state:指定任务计划present、absent

例子:

ansible all -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='ntpdate time.windows.com'"

也可以简写
ansible all -m cron -a "minute=0 name='同步时间' job='ntpdate time.windows.com'"  #其他默认为*

[root@ansible ~]# ansible zy.com -m cron -a "name='a job for echo time' minute=*/2 job='sh /opt/echo.sh'"

删除
[root@ansible ~]# ansible zy.com -m cron -a "name='a job for echo time' state=absent "

 

yum 模块
name参数:必须参数,用于指定需要管理的软件包,比如 nginx。
state参数:用于指定软件包的最终状态。present:表示安装,默认值。 latest:安装最新版本。absent:表示删除。

例子:

安装httpd
[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'name=httpd state=latest'

卸载
[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'name=httpd state=absent'

指定源安装

[root@localhost ~]# ansible 192.168.137.102 -m yum -a 'enablerepo=c7-dvd name=httpd'

 

get_url 模块

该模块主要用于从http,ftp,https,服务器上下载文件(类似于wget)主要有如下选项:

常用选项:

timeout:下载超时时间,默认10S

url:下载URL

dest:下载文件存放的绝对路径

url_password url_username 帐号密码验证。如何url_username 不指定,url_password不会使用。

use_proxy;使用代理,代理需事先在环境变更中定义。

[root@localhost ~]# ansible 192.168.137.102 -m get_url  -a 'url=http://mirrors.sohu.com/nginx/nginx-1.15.5.tar.gz dest=/tmp' 

 

猜你喜欢

转载自www.cnblogs.com/zyos/p/10189367.html