1、command

查看主机的时间

ansible -i /etc/ansible/hosts all -m command -a "date"

查看Nginx组的主机空间使用量

ansible -i  /etc/ansible/hosts nginx -m command -a "df -h"

查看mysql组主机的登录情况

ansible -i  /etc/ansible/hosts mysql -m command -a "who"

查看Nginx组的主机网卡

ansible -i  /etc/ansible/hosts nginx -m command -a "ifconfig"

检查主机是否能连接外网

ansible all  -m command -a "ping www.baidu.com -c 1"

2、ping

检查所有节点主机是否能连通

ansible -i  /etc/ansible/hosts all  -m ping
ansible all  -m ping

3、yum

参数:

config_file:yum的配置文件

disable_gpg_check:关闭gpg_check

disablerepo:不启用某个源

enablerepo:启用某个源

name:安装程序名(必须指定),也可以传递一个url或者一个本地的rpm包的路径 

state:状态(present-安装;latest-安装最新版;absent-卸载)


给所有主机安装更新ntpdate

ansible all -m yum -a "name=ntpdate state=latest"

卸载掉mysql组的wget

ansible mysql -m yum -a 'name=wget state=absent'

给指定主机安装开发包

ansible 192.168.174.80 -m yum -a 'name="@Development tools" state=present'

4、setup

查看apache组的主机的所有信息

ansible apache -m setup

查看指定主机的CPU信息,用filter参数指定

ansible 192.168.174.80 -m setup -a 'filter=*_processor

收集所有主机的信息输入到/tmp/ansible_host_message/目录下,每台主机的信息输入到主机名文件中(/etc/ansible/hosts里的主机名)

ansible all -m setup --tree /tmp/ansible_host_message/

5、file

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

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

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

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

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

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

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

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

state:  

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

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

    link:创建软链接

    hard:创建硬链接

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

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


在redis组主机下创建创建存放脚本的文件夹

ansible redis -m file -a "path=/home/sh state=directory"
ansible redis -m file -a "path=/home/sh state=directory owner=root group=root"

创建文件并赋权

ansible redis -m file -a "path=/home/sh/test.sh state=touch mode=0755"

创建软连接

ansible redis -m file -a "src=/home/sh/test.sh dest=/tmp/test.sh state=link"

删除

ansible redis -m file -a "path=/tmp/test.sh state=absent"

6、copy

src:要复制到远程主机的文件在本地的地址。路径是一个目录,它将递归复制。如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制。

dest:指定保存文件路径(必须指定)

mode:拷贝到远程主机上的文件权限

backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

ansible redis -m copy -a "src=/home/sh/test.sh dest=/home/sh/test.sh mode=0755"
ansible redis -m copy -a "src=/home/sh/test.sh dest=/home/sh/test1.sh mode=0755"
ansible redis -m copy -a "src=/home/sh/test.sh dest=/home/sh/test.sh mode=0755 owner=www group=www"

7、shell

与command有点类似,支持shell特性,如管道等。


查看redis组的主机空间使用量

ansible redis -m shell -a "df -h"
ansible -i  /etc/ansible/hosts redis -m shell -a "df -h"

查看redis等进程是否存在

ansible redis -m shell -a "ps aux | grep redis"

执行/home/sh/test.sh脚本并把结果输出到/home/sh/test.txt

ansible redis -m shell -a "sh /home/sh/test.sh >> /home/sh/test.txt"
ansible redis -m shell -a "sh test.sh >> test.txt chdir=/home/sh/"

8、script

将本机脚本/home/sh/test.sh传送到远程主机redis组并执行,把结果输出到/home/sh/test.txt

ansible redis -m script -a "/home/sh/test.sh >> /home/sh/test.txt"

9、user

参数:

name:指定用户名(必须指定)

state:present-添加用户;absent-删除用户

remove:当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r

system:是否创建为系统用户,有两个选项:yes|no

uid:指定用户uid

shell:指定用户shell环境

home:指定用户家目录

group:指定用户组

groups:指定用户组附加组,以”,“分隔


创建用户

ansible redis -m user -a 'name=test state=present system=no uid=1111 groups=root,test shell=/bin/bash home=/home/test_home'

删除用户

ansible redis -m user -a 'name=test state=absent'
ansible redis -m user -a 'name=test state=absent remove=yes'

10、cron

name:任务计划的描述信息(必须指定)

minute:分(0-59 ,* ,*/2)

hour:时(0-23 ,* ,*/2)

day:日(1-31 ,* ,*/2)

month:月(1-12 ,* ,*/2)

weekday:周(0-6 ,*)

job:执行的命令的绝对路径

backup:是否先备份再创建新的任务计划

user:以哪个用户新建任务计划,默认 root

state:present-创建计划任务;absent-删除计划任务


给所有主机创建同步时间定时计划

(同步NTP服务器可在这个网址找http://www.ntp.org.cn/)

ansible all -m cron -a 'name=ntpdate user=root job="/usr/sbin/ntpdate 58.220.207.226" hour="*/6" state=present'

删除定时计划

ansible redis -m cron -a 'name=ntpdate state=absent'

11、service

用于管理服务

参数:

name:必选项,服务名称 

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

enabled:是否开机启动 yes|no

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

runlevel:运行级别

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

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


重启Apache

ansible apache -m service -a "name=httpd state=restarted enabled=yes"

关闭防火墙

#centos 7 
ansible nginx -m service -a "name=firewalld state=stopped enabled=no"
#centos 6 及以下
ansible nginx -m service -a "name=iptables state=stopped enabled=no"

重启网卡,args指定网卡

ansible test -m service -a "name=network state=restarted args=ens33"

12、ansible playbook

我们使用如上这些命令可以快速利用Ansible的工具编写脚本、从而以非常简便的方式实现任务处理的自动化与流程化。

除此之外,我们还可以创建Ansible Playbook以收集命令与任务集,这样能够大大降低管理工作的复杂程度。

Playbook采用YAML语法结构,因此它们一般比较易于阅读并加以配置,如下简单案例使用playbook实现在客户端安装screen软件。

在/etc/ansible/目录下,新建screen_install.yaml文件,内容如下:

(冒号后面有内容的,要加一个空格隔开)

- hosts: all 
  remote_user: root
  tasks:
  - name: yum install screen 
    shell: yum install screen –y

文件解析:

-hosts:all指定对所有hosts生效;

remote_user表示远程root;

tasks表示需要执行的任务;

name显示的名称;

shell后面接:需要在远程客户端执行的命令。可以写多个命令,以;分号隔开即可,例如 shell: yum install screen -y ;mkdir /tmp/`date +%Y%m%d`

执行

ansible-playbook screen_install.yaml