二、ansible常用模块

1、查看所有ansible所支持模块

[root@localhost ~]# ansible-doc -l

Command ---远程主机指定shell命令的

Crond  ---管理远程主机的定时任务

file 用来设置远程主机文件属性

filesystem 用来在远程主机上创建文件系统

group 在远程主机上创建一个用户组

User 在远程主机上创建一个用户

shell 在远程主机上指定命令的

setup 收集远端主机接口类的基本信息、系统类型,版本

service在远程主机上管理命令的

selinux 用来管理远程主机的selinux的

yum  用来管理远程主机软件包的

2、ansible-doc -s 指定模块  查看这个模块是怎么使用的

ansible-doc -s user

[root@localhost ~]# ansible-doc -s user

- name: Manage user accounts

  action: user

      append                 # If `yes', will only add groups, not set them to just the list in

                               `groups'.

      comment                # Optionally sets the description (aka `GECOS') of user account.

      createhome             # Unless set to `no', a home directory will be made for the user

                               when the account is created or if

                               the home directory does not exist.

      expires                # An expiry time for the user in epoch, it will be ignored on

                               platforms that do not support this.

                               Currently supported on Linux and

                               FreeBSD.

      force                  # When used with `state=absent', behavior is as with `userdel

                               --force'.

      generate_ssh_key       # Whether to generate a SSH key for the user in question. This will

                               *not* overwrite an existing SSH

                               key.

3、模块使用

 

1、-m setup   收集客户端的一些信息

ansible facts a s i b ] e all i pv4 ad r e s s e s ' 192 . 168 . 122 . 1 " ' 192 . 168 . 16 . 141 " ansible all i pv6 addresses "fe80 : : 20C : 29ff : fe 6 b : 6249 " ansible apparmor " 小 sabl ed" S t a t u S m 0 r e 一 514 . 6 . 1 . e ] 7 . x86 64 " ansible architecture ansible 舅 os date bIOS V e r S 1 0 n ' x86 64 " " 6 . 00 " ansible cmdline 'BOOT I MAG E LANG crashkernel q u 1 e t t r u e 'rhgb ' t r u e t r u e " /vml i nuz-3 . 10 . 0 a u t 0 ' UI-JID = If8b4afd 一 e735 一 432a 一 9ad7 一 480e8984e835 " r00t ansible date t 1 me 'date ' 2017 一 09 一 17 " 'day" epoch" ' 1505710927 " s08601 " ' 2017 一 09 一 18T05 : 02 : 07Z is08601—basic ' 20170917T220207805690 " is08601—basic short ' 20170917T220207 " is08601—micro ' 2017 一 09 一 18T05 : 02 : 07 . 805797Z ' " v:shapes="图片_x0020_8">

2、-m ping

检查主机是否存活

[root@localhost ~]# ansible test -m ping

192.168.16.139 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

192.168.16.141 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

[root@localhost ~]#

 

3、file模块

file模块主要用于远程主机上的文件操作,file模块包含如下选项: 

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

使用示例:

ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link"  link链接文件

ansible test -m file -a "path=/tmp/fstab state=absent"     absent删除

ansible test -m file -a "path=/tmp/test state=touch" 创建一个文件

 

4copy模块

复制文件到远程主机,copy模块包含如下选项:

  • backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no 
  • content:用于替代"src",可以直接设定指定文件的值 
  • dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录 
  • directory_mode:递归的设定目录的权限,默认为系统默认权限
  • force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
  • others:所有的file模块里的选项都可以在这里使用
  • src:要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用"/"来结尾,则只复制目录里的内容,如果没有使用"/"来结尾,则包含目录在内的整个内容全部复制,类似于rsync。 
  • validate :The validation command to run before copying into place. The path to the file to validate is passed in via '%s' which must be present as in the visudo example below.

示例:

ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"

ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"

ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"4

 

[root@localhost ~]# ansible test -m copy -a 'src=anaconda-ks.cfg dest=/tmp/anaconda-ks.cfg mode=755 owner=root group=root'

192.168.16.139 | SUCCESS => {

    "changed": true,

    "checksum": "1d1dbb4675724033c17327538b46ccb0028ba313",

    "dest": "/tmp/anaconda-ks.cfg",

    "gid": 0,

    "group": "root",

    "md5sum": "51984625b2df7627bc72840f7faabbdc",

    "mode": "0755",

    "owner": "root",

    "size": 2882,

    "src": "/root/.ansible/tmp/ansible-tmp-1505712442.48-66368253271207/source",

    "state": "file",

    "uid": 0

}

192.168.16.141 | SUCCESS => {

    "changed": true,

    "checksum": "1d1dbb4675724033c17327538b46ccb0028ba313",

    "dest": "/tmp/anaconda-ks.cfg",

    "gid": 0,

    "group": "root",

    "md5sum": "51984625b2df7627bc72840f7faabbdc",

    "mode": "0755",

    "owner": "root",

    "size": 2882,

    "src": "/root/.ansible/tmp/ansible-tmp-1505712442.49-264288329524275/source",

    "state": "file",

    "uid": 0

}

 

 

Backup参数-

'backup fil e ' " /tmp/anaconda-ks . cfg . 40697 . 2017 一 09 一 17@22 : 3 6 : 30 、 'changed" t r u e checksum " 7d15e2186da4aeb6f7972e896ee460e25ee48274 " dest group md 5 s u m mode owner S 1 Ze S r C S t a t e 下 d " " /tmp/anaconda-ks . cfg" r00t ' 133f8bd409C706e845ae51723a3d6288 " " 0755 " r00t 2885 , ' /root/ . ansi bl e/tmp/ansi bl e 一 tmp 一 1505712989 . 75 一 89197838658265 / source 'fil e ' t e S t sep sep 一 m command 一 ] S 一 ] h /tmp/' 8 13 : 3 6 anaconda-ks . cfg 8 13 : 2 7 anaconda-ks.cfg. 51851 . 2017 一 09 一 18@13 : 36 : 30 、 Croot@localhost 、 ] ansible 192 . 168 . 16 . 139 1 S total 8 . 0K 一 rwx r 一 X r 一 X 1 root root 2 . 9K 一 rwx r 一 X r 一 X 1 root root 2 . 9K " v:shapes="图片_x0020_7">

 

5、command模块

command 模块用于运行系统命令,比如echo hello, 你安装在系统里的python,或者make 一类。大家能领悟就行了。

常用参数:

parameter

required

default

choices

comments

chdir

no

 

 

运行command命令前先cd到这个目录

creates

no

 

 

如果这个参数对应的文件存在,就不运行command

executable

no

 

 

将shell切换为command执行,这里的所有命令需要使用绝对路径

removes

no

 

 

如果这个参数对应的文件不存在,就不运行command

案例:

#ansible 命令调用command:
ansible -i hosts all -m command -a "/sbin/shutdown -t now"

 

 

 

6、shell/raw模块

Shell/raw是command的一个扩展,比command命令功能更强大!!但是官方建议能使用command就使用command

 

7、service模块

主要是管理服务的加载,停止 启动

  •  
  • enabled:是否开机启动 yes|no
  • name:必选项,服务名称 
  • state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

 

示例:

ansible test -m service -a "name=httpd state=started enabled=yes"

 

[root@localhost ansible]# ansible test -m service -a "name=httpd state=started enabled=yes"

192.168.16.136 | SUCCESS => {

    "changed": false,

    "enabled": true,

    "name": "httpd",

    "state": "started",

    "status": {

        "ActiveEnterTimestamp": "Wed 2017-09-20 01:21:52 PDT",

        "ActiveEnterTimestampMonotonic": "6697948546",

        "ActiveExitTimestamp": "Wed 2017-09-20 01:21:22 PDT",

        "ActiveExitTimestampMonotonic": "6667963515",

        "ActiveState": "active",

 

[root@localhost ansible]# ansible test -m raw -a  'ps -ef | grep 80'

192.168.16.136 | SUCCESS | rc=0 >>

root        280      2  0 Sep19 ?        00:00:00 [xfs-cil/sda3]

root        380      2  0 Sep19 ?        00:00:00 [rpciod]

root        480      2  0 Sep19 ?        00:00:00 [xfs-log/sda1]

root       7889   7887  0 02:22 pts/2    00:00:00 bash -c ps -ef | grep 80

root       7902   7889  0 02:22 pts/2    00:00:00 grep 80

Shared connection to 192.168.16.136 closed.

 

192.168.16.139 | SUCCESS | rc=0 >>

root         80      2  0 10:57 ?        00:00:00 [rcuob/71]

root         89      2  0 10:57 ?        00:00:00 [rcuob/80]

root        180      2  0 10:57 ?        00:00:00 [rcuos/42]

root        218      2  0 10:57 ?        00:00:00 [rcuos/80]

root        280      2  0 10:57 ?        00:00:00 [khungtaskd]

root        802      1  0 10:57 ?        00:00:00 /sbin/rngd -f

root        803      1  0 10:57 ?        00:00:00 /usr/sbin/abrtd -d -s

root        804      1  0 10:57 ?        00:00:00 /usr/bin/abrt-watch-log

 

 

 

 

 

8、cron模块

用于管理计划任务包含如下选项: 

backup:对远程主机上的原任务计划内容修改之前做备份 

cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 

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

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

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

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

weekday:周(0-7,*,……)

job:要执行的任务,依赖于state=present 

name:该任务的描述 

special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly 

state:确认该任务计划是创建还是删除 

user:以哪个用户的身份执行

 

示例:

ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'

ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root

ansible test -m cron  -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'

ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'

 

每隔两小时

 ansible test -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot"'

192.168.16.139 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": [

        "reboot system"

    ]

}

 

[root@localhost ansible]# ansible test -a 'crontab -l'

192.168.16.139 | SUCCESS | rc=0 >>

* 2 * * * /sbin/reboot

 

 

 

[root@localhost ansible]#  ansible test -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot" state=absent'

192.168.16.139 | SUCCESS => {

    "changed": false,

    "envs": [],

    "jobs": []

}

 

[root@localhost ansible]#  ansible test -m cron -a 'name="reboot system" state=absent'

192.168.16.139 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": []

}

[root@localhost ansible]# ansible test -a 'crontab -l'

192.168.16.139 | SUCCESS | rc=0 >>

 

 

 

参数

必填

默认

选择

说明

Backup

 

Yes/no

如果yes,那么在修改之后会进行备份,备份的路径在backup_file

Cron_file

 

 

如果设置了,那么在cron.d中使用此文件替代单独用户的crontab,在使用此选项的时候,必须使用user选项

Day

 

 

Hour

 

 

小时 ( 0-23, *, */2, etc )

Job

 

 

需要执行的命令,必须状态为present

Minute

 

 

分 ( 0-59, *, */2, etc )

Month

 

 

月( 1-12, *, */2, etc )

Name

 

 

任务的描述

Reboot

No

Yes/no

重启后是否需要执行

Special_time

 

reboot

yearly

annually

monthly

weekly

daily

hourly

特定的执行时间

State

Present

Present

Absent

启用或停用任务

User

Root

 

执行任务的用户

Weekday

 

 

每一周的哪天进行运行(0-6 for Sunday-Saturday, *, etc)

 

#在远程主机部署/删除cron任务

在重启时运行job.sh

ansible -i hosts all -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'

ansible -i hosts all -m cron -a 'backup="True" name="test" minute="0" hour="2" job="ls -alh > /dev/null"'

ansible -i hosts all -m cron -a 'name="test" state=absent'

 

 

9、yum

- name: 安装最新版本的apache yum: name=httpd state=latest

- name: 移除apache yum: name=httpd state=absent

- name: 安装一个特殊版本的apache yum: name=httpd-2.2.29-1.4.amzn1 state=present

- name: 升级所有的软件包 yum: name=* state=latest

- name: 从一个远程yum仓库安装nginx

yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present

- name: 从本地仓库安装nginx

 yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present - name: 安装整个Development tools相关的软件包

yum: name="@Development tools" state=present

 

 

[root@localhost ansible]# ansible test -m yum -a 'name=httpd state=absent'

192.168.16.136 | SUCCESS => {

    "changed": true,

    "msg": "",

    "rc": 0,

    "results": [

 

[root@localhost ansible]# ansible test -m yum -a 'name=httpd state=latest'

 

 

10、user/group模块

 

创建组

 

[root@localhost ansible]#  ansible mfs_138 -m group -a "name=hanjj"

192.168.16.138 | SUCCESS => {

    "changed": true,

    "gid": 1001,

    "name": "hanjj",

    "state": "present",

    "system": false

}

 

用户加入到组

 

[root@localhost ansible]#  ansible mfs_138 -m user -a "name=ha group=root password=123456"

192.168.16.138 | SUCCESS => {

    "changed": true,

    "comment": "",

    "createhome": true,

    "group": 0,

    "home": "/home/ha",

    "name": "ha",

    "password": "NOT_LOGGING_PASSWORD",

    "shell": "/bin/bash",

    "state": "present",

    "system": false,

    "uid": 1001

}

 

 

删除用户

[root@localhost ansible]#  ansible mfs_138 -m user -a "name=han state=absent"

192.168.16.138 | SUCCESS => {

    "changed": true,

    "force": false,

    "name": "han",

    "remove": false,

    "state": "absent"

}

 

 

十一、mount模块

配置挂载点

选项: 

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

示例:

 

 

挂载光盘

[root@localhost ansible]# ansible mfs_138 -m mount -a 'name=/mnt src=/dev/sr0 fstype=iso9660 state=mounted opts=ro'

 

卸载光盘

[root@localhost ansible]# ansible mfs_138 -m mount -a 'name=/mnt state=unmounted'

 

 

十二、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 mfs_136 -m file -a "path=/root/test11 mode=755 owner=root group=root state=touch"

[root@localhost ~]# ansible mfs_136 -m file -a "path=/root/test11 mode=755 owner=root group=root state=directory"

 

删除文件目录

[root@localhost ~]# ansible mfs_136 -m file -a "path=/root/test11 mode=755 owner=root group=root state=absent"

[root@localhost ~]# ansible mfs_136 -m file -a "path=/root/test11 mode=755 owner=root group=root state=absent"

 

创建软链接

[root@localhost ~]# ansible mfs_136 -m file -a "src=/root/test11 dest=/mnt/test11 state=link"

查看链接文件

[root@localhost ~]#  ansible mfs_136  -a 'ls -ll /mnt/test11'

192.168.16.136 | SUCCESS | rc=0 >>

lrwxrwxrwx 1 root root 12 Sep 26 05:32 /mnt/test11 -> /root/test11

 

 

十三、 synchronize

服务端上的文件/目录  做同步,重点功能解析

1、archive 归档

2、checksum 检验校验和

3、delete 删除源没有,目标存在的文件

4、dest目标到哪里去

5、dest_port 目标接受端口

6、dirs递归传递目录

7、mode rsync传递的方式两种 push  服务端-->客户端   pull  服务端<----客户端

8、recursive是否递归

 

举例

1、同步文件过去

[root@localhost ~]# ansible mfs_138 -m synchronize -a 'src=/root/helloworld dest=/var/www/html/helloworld'

 

2、同步整个目录,利用rsync去往客户端上推送

[root@localhost ~]# ansible mfs_138 -m synchronize -a 'src=/root/test dest=/var/www/html/ mode=push'

192.168.16.138 | SUCCESS => {

 

 

 

 

 

 

 

 

知识汇总

1、远程挂载镜像

 

[root@localhost ansible]# ansible mfs_138 -m mount -a "path=/mnt src=/dev/sr0 fstype=iso9660 state=mounted opts=ro"

卸载镜像需要

ansible mfs_138 -m mount -a "name=/mnt state=unmonted"

2、根据镜像远程yum 安装

 

[root@localhost ansible]# ansible mfs_136 -m yum -a "name=httpd state=latest"

3、远程service启动

[root@localhost ansible]# ansible mfs_138 -m service -a 'name=httpd state=started'

 

4、远程查看服务运行状态

[root@localhost ansible]# ansible mfs_138 -m raw -a 'ps -ef | grep httpd'

[root@localhost ansible]# ansible mfs_138 -m raw -a 'lsof -i:80'

 

5、远程停止服务

root@localhost ansible]# ansible mfs_138 -m service -a 'name=httpd state=stopped'

 

6、远程移除服务

[root@localhost ~]# ansible mfs_138 -m yum -a 'name=httpd state=removed'

 

7、远程创建组

[root@localhost ~]# ansible mfs_138 -m group -a 'name=hjz'

 

8、远程创建用户

[root@localhost ~]# ansible mfs_138 -m user -a 'name=hjz password=123456 group=hjz'

 

9、远程移除用户

[root@localhost ~]# ansible mfs_138 -m user -a 'name=hjz state=absent'

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/hanjingzheng/p/9082402.html