1. 查看支持的模块

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

这里我们看下ansible的支持的模块个数

[root@localhost ~]# ansible-doc -l |wc -l   #查看支持的模块个数

[root@localhost ~]# ansible --version        #查看我们的ansible版本号
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.6. (r266:, Aug , ::) [GCC 4.4. (Red Hat 4.4.-)]

2.获取模块的帮助

这里我们使用ansible-doc获取下command模块的使用方式。

[root@localhost ~]# ansible-doc command

3.1 command模块

command :作为ansible的默认模块,可以允许远程主机范围内的所有shell命令。

注意: 在command的命令中含有像`$ HOME'这样的变量和像``<“',`”>“, `“”“”,“”;“”和“”&“'将无法正常工作(如果需要这些功能,请使用[shell]模块)

[root@localhost ~]# ansible 192.168.168.11* -m command -a 'ip addr show dev eth0'
192.168.168.115 | SUCCESS | rc= >>
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether ::::8d:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.168.115/ brd 192.168.168.255 scope global eth0
inet6 fe80:::56ff:fe29:8de2/ scope link
valid_lft forever preferred_lft forever 192.168.168.111 | SUCCESS | rc= >>
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::: brd ff:ff:ff:ff:ff:ff
inet 192.168.168.111/ brd 192.168.168.255 scope global eth0
inet6 fe80::20c:29ff:fe77:/ scope link
valid_lft forever preferred_lft forever

3.2 script模块

功能:在远程主机上执行主控端的脚本,相当于scp+shell组合。

[root@localhost ~]# ansible all -m script -a "/home/test.sh 12 34"

3.3 shell模块

功能:执行远程主机的shell脚本文件

[root@localhost ~]# ansible all -m shell -a "/home/test.sh"

shell替代command执行

[root@localhost ~]# ansible 192.168.168.11* -m shell -a 'ip addr show dev eth0'
192.168.168.111 | SUCCESS | rc= >>
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c:::: brd ff:ff:ff:ff:ff:ff
inet 192.168.168.111/ brd 192.168.168.255 scope global eth0
inet6 fe80::20c:29ff:fe77:/ scope link
valid_lft forever preferred_lft forever 192.168.168.115 | SUCCESS | rc= >>
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether ::::8d:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.168.115/ brd 192.168.168.255 scope global eth0
inet6 fe80:::56ff:fe29:8de2/ scope link
valid_lft forever preferred_lft forever

3.4 copy模块

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

[root@localhost ~]# ansible all -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"    
#src 主控端文件位置
#dest 被控端目标位置
#owner 文件复制过去后的所有者
#group 文件复制过去后的所属组
#mode 文件的权限设定,执行a+x这种方式

3.5 stat模块

功能: 获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息。

[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"

3.6 yum模块

功能: 安装软件包。

[root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
#name 包名
#state (Choices: present, installed, latest, absent, removed)[Default: present]
#disable_gpg_check:禁止gpg检查
#enablerepo:只启动指定的repo

3.7 cron模块

功能:远程主机crontab配置

[root@localhost ~]# ansible all -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=tom"
192.168.168.115 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}
192.168.168.111 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}

我们去被控主机看下生成的crontab作业

[root@localhost ~]# crontab  -l -u tom
#Ansible: test
*/ - , ls -l

删除指定crontab

[root@localhost ~]# ansible all -m cron -a "name=test state=absent"

3.8 mount模块

功能: 挂载文件系统

[root@localhost ~]# ansible 192.168.168.111 -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext3 ots=ro state=present"

注:mount已经使用path代替了原来的name参数,但是name参数还是可以使用的。

3.9 service模块

功能: 服务管理

[root@localhost ~]# ansible all -m service -a "name=httpd state=restarted"    #启动服务
[root@localhost ~]# ansible all -m service -a "name=httpd state=running" #查看服务状态
[root@localhost ~]# ansible all -m service -a "name=httpd state=stoped" #停止服务

3.10 user模块

功能: 远程主机的用户管理

[root@localhost ~]# ansible all -m user -a "name=jerry comment=' doubi jerry'"   #添加用户 详细参数参考ansible-doc user
[root@localhost ~]# ansible all -m user -a "name=jerry state=absent remove=yes" #删除用户

ansible学习系列2-ansible常用模块使用的更多相关文章

  1. SaltStack学习系列之state常用模块

    常用模块:cron,cmd,file,mount,ntp,pkg,service,user,group cmd模块 参数: name:要执行的命令 unless:用于检查的命令,只有unless指向的 ...

  2. Python学习系列(六)(模块)

    Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...

  3. 自动化运维工具Ansible实战(四)常用模块

    转载链接:http://blog.51cto.com/liqingbiao/1962609   Ansible模块按功能分为:云模块.集群模块. 命令模块.数据库模块.文件模块.资产模块.消息模块.监 ...

  4. Func系列2:常用模块及API

    简介 Func提供了非常丰富的功能模块,包括CommandModule(执行命令).CopyFileModule(拷贝文件).CPUModule(CPU信息).DiskModule(磁盘信息).Fil ...

  5. Django学习之六:Django 常用模块导入记忆

    Django 常用模块导入记忆 django相关 1. urls相关操作 from django.urls import path, re_path, include from django.urls ...

  6. Python学习—基础篇之常用模块

    常用模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  7. Ansible 快速安装配置,常用模块

    Ansible是一个轻量级的工具,基于python语言实现,通过python中的paramiko来连接并管理机器, 功能强大(YAML,PlayBook,模块化功能),不需要安装客户端, 通过ssh连 ...

  8. Ansible笔记(7)---常用模块之系统类模块(cron、service)

    一.cron模块 1.1作用: cron 模块可以帮助我们管理远程主机中的计划任务,功能相当于 crontab 命令. 在了解cron模块的参数之前,先写出一些计划任务的示例: # 示例1,每天的1点 ...

  9. Ansible笔记(2)---常用模块之文件操作

    一.copy模块 1.1作用: copy模块是将ansible主机上的文件拷贝到远程受控主机 1.2常用参数: src参数 :用于指定需要copy的文件或目录. dest参数 :用于指定文件将被拷贝到 ...

随机推荐

  1. postgresql从timestamp(6)复制到timestamp(0),时间会变

    主要涉及临界点(跨天) 例子(时间:2016-08-05 23:59:59.863) timestamp(6):2016-08-05 23:59:59.863 timestamp(0):2016-08 ...

  2. web 10

    一.Iterations : 1.do...while : 创建执行指定语句的循环,直到测试条件评估为false.在执行语句后评估条件,导致指定语句至少执行一次. 例子:在以下示例中,do...而循环 ...

  3. 关于Http协议,你必须要知道的

    转自:https://segmentfault.com/a/1190000016751071 引言 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用 ...

  4. SQL插入数据--数据中的某一列来自本表中的数据

    背景: 项目初期使用的配置库和业务库两种数据库并行,所有配置数据位于配置库,所有业务数据根据不同省份位于不同数据库.由于使用省份越来越多,各省的配置数据也越来越多导致配置库的数据量过于庞大,各省共用一 ...

  5. .NET Core跨平台的奥秘[下篇]:全新的布局

    从本质上讲,按照CLI规范设计的.NET从其出生的那一刻就具有跨平台的基因,这与Java别无二致.由于采用了统一的中间语言,微软只需要针对不同的平台设计不同的虚拟机(运行时)就能弥合不同操作系统与处理 ...

  6. JAVA基础—适配器设计模式

    适配器概念 在计算机编程中,适配器模式将一个类的接口适配成用户所期待的.使用适配器,可以使接口不兼容而无法在一起工作的类协调工作,做法是将类自己包裹在一个已经存在的类中. JDK对适配器设计模式的应用 ...

  7. [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. [Swift]LeetCode231. 2的幂 | Power of Two

    Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...

  9. [Swift]LeetCode476. 数字的补数 | Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  10. [Swift]LeetCode791. 自定义字符串排序 | Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...