Ansible常用模块

官网地址:http://docs.ansible.com/synchronize_module.html

常用模块

  • copy模块
  • file模块
  • cron模块
  • group模块
  • user模块
  • yum模块
  • service模块
  • script模块
  • ping模块
  • command模块
  • raw模块
  • get_url模块
  • synchronize模块
  • setup:

查看可用的模块

Ansible-doc –l

查看使用方法

Ansible-doc –s copy

Command模块

目的

能在远程节点运行一个命令,但不能运行有管理的命令,command模块的参数非淖kv格式,而是直接给出要执行的命令.

示例

ansible all -m command -a "date"

ping 模块

目的

用于做测试

示例

ansible all -m ping

[root@ansible1 ~]# ansible web1 -m command -a 'date'
192.168.1.9 | SUCCESS | rc=0 >>
2016年 09月 08日 星期四 21:28:00 CST

192.168.1.11 | SUCCESS | rc=0 >>
2016年 09月 08日 星期四 21:28:00 CST

192.168.1.13 | SUCCESS | rc=0 >>
2016年 09月 08日 星期四 21:28:00 CST

user模块

目的

管理用户

选项

  • name = 创建的用户名
  • state= present新增,absent删除
  • force= 删除用户的时候删除家目录
  • system= 创建系统用户
  • uid= 指定UID
  • shell= 指定shell
  • home= 指定用户家目录

示例

ansible webserver -m user -a "name=luck state=present system=true"
[root@ansible1 ~]# ansible web1 -m user -a 'name=test state=present uid=8888 shell=/sbin/nologin home=/test'
192.168.1.13 | SUCCESS => {
"changed": true, 
"comment": "", 
"createhome": true, 
"group": 8888, 
"home": "/test", 
"name": "test", 
"shell": "/sbin/nologin", 
"state": "present", 
"system": false, 
"uid": 8888
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"comment": "", 
"createhome": true, 
"group": 8888, 
"home": "/test", 
"name": "test", 
"shell": "/sbin/nologin", 
"state": "present", 
"system": false, 
"uid": 8888
}
192.168.1.11 | SUCCESS => {
"changed": true, 
"comment": "", 
"createhome": true, 
"group": 8888, 
"home": "/test", 
"name": "test", 
"shell": "/sbin/nologin", 
"state": "present", 
"system": false, 
"uid": 8888
}

group 模块

目的

管理组

选项

  • gid: 指定gid
  • name= 指定组名称
  • state 操作状态 ,present,absent
  • system:是否为系统组

示例

ansible all –m group –a “name=luck state=present ”
192.168.1.11 | SUCCESS => {
"changed": true, 
"gid": 10001, 
"name": "luck", 
"state": "present", 
"system": false
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"gid": 10001, 
"name": "luck", 
"state": "present", 
"system": false
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"gid": 10001, 
"name": "luck", 
"state": "present", 
"system": false
} # cron模块 #

目的

管理计划任务

选项

  • name= 任务名称
  • state= 删除或新增
  • minute= 分钟
  • hour= 小时
  • day= 天
  • month= 月
  • weekday= 周
  • job= 任务

示例

ansible all -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 10.1.250.95 &> /dev/null' name='sync time' state=present"
[root@ansible1 ~]# ansible web1 -m cron -a "minute=*/5 job='sh /root/touch.sh' name='touch' state=present"
192.168.1.11 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"touch"
]
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"touch"
]
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"envs": [], 
"jobs": [
"touch"
]
}
查看
Ansible: touch
*/5 * * * * sh /root/touch.sh

file模块

目的

文件创建和删除 ## 选项## - path= 目标文件路径 - mode= 权限 - owner= 属主 - group=属组 - state= 执行状态,directory,link,hard,touch,file,absent - src= 源文件 

示例

ansible all -m file -a 'path=/root/fstab src=/etc/fstab state=link'
[root@ansible1 ~]# ansible web1 -m file -a 'path=/root/fstab src=/etc/fstab state=link'
192.168.1.9 | SUCCESS => {
"changed": true, 
"dest": "/root/fstab", 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"size": 10, 
"src": "/etc/fstab", 
"state": "link", 
"uid": 0
}
192.168.1.11 | SUCCESS => {
"changed": true, 
"dest": "/root/fstab", 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"size": 10, 
"src": "/etc/fstab", 
"state": "link", 
"uid": 0
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"dest": "/root/fstab", 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"size": 10, 
"src": "/etc/fstab", 
"state": "link", 
"uid": 0
}
[root@ansible2 ~]# ll 
总用量 4
lrwxrwxrwx 1 root root 10 9月   8 22:00 fstab -> /etc/fstab # copy 模块 # ## 	目的## 文件复制 ## 选项 ## - src= 源路径 - dest= 目标路径 - mode= 权限 - owner: 属主

示例

ansible all -m copy -a 'src=/etc/issue dest=/tmp/aaa'

yum 模块

目的

程序管理

选项:

  • name= 软件名称
  • conf_file= yum repo 配置文件
  • state= 执行状态,present,lasent,absent
  • enablerepo= 启动指定仓库
  • disablerepo= 关闭指定的仓库
  • disable_gpg_check= false

示例

ansible localhost -m yum -a 'name=nginx state=present'

service模块

服务管理

选项

  • name= 服务名称
  • state= 服务执行动作(stared,stopped,restarted,reload)
  • enabled= 是否开启自启动
  • Sleep: 当restart时,stop时指定sleep时间

示例

ansible localhost -m service -a 'name=nginx state=started enabled=true'
[root@ansible1 ~]# ansible web1 -m service -a 'name=crond state=started enabled=true'
192.168.1.11 | SUCCESS => {
"changed": true, 
"enabled": true, 
"name": "crond", 
"state": "started"
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"enabled": true, 
"name": "crond", 
"state": "started"
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"enabled": true, 
"name": "crond", 
"state": "started"
}

shell模块

目的

运行shell命令,其是启动一个子shell进程来运行铭记,它可以支持管道传送

选项

  • chdir= 执行命令时,切换到指定的目录下
  • command 不支持管道

示例

ansible all -m shell -a 'echo "123"|passwd --stdin luck'
[root@ansible1 ~]# ansible web1 -m shell -a 'echo "123"|passwd --stdin luck'
192.168.1.9 | SUCCESS | rc=0 >>
更改用户 luck 的密码 。
passwd:所有的身份验证令牌已经成功更新。

192.168.1.11 | SUCCESS | rc=0 >>
更改用户 luck 的密码 。
passwd:所有的身份验证令牌已经成功更新。

192.168.1.13 | SUCCESS | rc=0 >>
更改用户 luck 的密码 。
passwd:所有的身份验证令牌已经成功更新。

script 模块

目的

在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)

示例

[root@ansible1 ~]# ansible web1 -m script -a '/root/touch.sh'
192.168.1.11 | SUCCESS => {
"changed": true, 
"rc": 0, 
"stderr": "", 
"stdout": "", 
"stdout_lines": []
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"rc": 0, 
"stderr": "", 
"stdout": "", 
"stdout_lines": []
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"rc": 0, 
"stderr": "", 
"stdout": "", 
"stdout_lines": []
} # get_url模块 # ## 目的 ##

将http://www.rqbao.com/static_3/image/banner2.png文件下载到指定节点的/tmp目录下

示例

[root@ansible1 ~]# ansible web1 -m get_url -a 'url=http://ocppiicaw.bkt.clouddn.com/dog.png dest=/tmp'
192.168.1.11 | SUCCESS => {
"changed": true, 
"checksum_dest": null, 
"checksum_src": "12433ccc773f64e1019b84f80c6390a89a9a0766", 
"dest": "/root/dog.png", 
"gid": 0, 
"group": "root", 
"md5sum": "d17aba787bf94e9d2c46d876c58091e0", 
"mode": "0644", 
"msg": "OK (32829 bytes)", 
"owner": "root", 
"size": 32829, 
"src": "/tmp/tmpmmXvsw", 
"state": "file", 
"uid": 0, 
"url": "http://ocppiicaw.bkt.clouddn.com/dog.png"
}
192.168.1.13 | SUCCESS => {
"changed": true, 
"checksum_dest": null, 
"checksum_src": "12433ccc773f64e1019b84f80c6390a89a9a0766", 
"dest": "/root/dog.png", 
"gid": 0, 
"group": "root", 
"md5sum": "d17aba787bf94e9d2c46d876c58091e0", 
"mode": "0644", 
"msg": "OK (32829 bytes)", 
"owner": "root", 
"size": 32829, 
"src": "/tmp/tmpLSQJfw", 
"state": "file", 
"uid": 0, 
"url": "http://ocppiicaw.bkt.clouddn.com/dog.png"
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"checksum_dest": null, 
"checksum_src": "12433ccc773f64e1019b84f80c6390a89a9a0766", 
"dest": "/root/dog.png", 
"gid": 0, 
"group": "root", 
"md5sum": "d17aba787bf94e9d2c46d876c58091e0", 
"mode": "0644", 
"msg": "OK (32829 bytes)", 
"owner": "root", 
"size": 32829, 
"src": "/tmp/tmp2WThfK", 
"state": "file", 
"uid": 0, 
"url": "http://ocppiicaw.bkt.clouddn.com/dog.png"
}

synchronize模块

目的

将主控方/root/a目录推送到指定节点的/tmp目录下

选项

  • delete=yes 使两边的内容一样(即以推送方为主)
  • compress=yes 开启压缩,默认为开启
  • –exclude=.git 忽略同步.git结尾的文件

示例

[root@ansible1 ~]# ansible web1 -m synchronize -a 'src=/root/luck dest=/opt compress=yes'
192.168.1.13 | SUCCESS => {
"changed": true, 
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/luck\" \"192.168.1.13:/opt\"", 
"msg": "cd+++++++++ luck/\n<f+++++++++ luck/wyy\n", 
"rc": 0, 
"stdout_lines": [
"cd+++++++++ luck/", 
"<f+++++++++ luck/wyy"
]
}
192.168.1.9 | SUCCESS => {
"changed": true, 
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/luck\" \"192.168.1.9:/opt\"", 
"msg": "cd+++++++++ luck/\n<f+++++++++ luck/wyy\n", 
"rc": 0, 
"stdout_lines": [
"cd+++++++++ luck/", 
"<f+++++++++ luck/wyy"
]
}
192.168.1.11 | SUCCESS => {
"changed": true, 
"cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/root/luck\" \"192.168.1.11:/opt\"", 
"msg": "cd+++++++++ luck/\n<f+++++++++ luck/wyy\n", 
"rc": 0, 
"stdout_lines": [
"cd+++++++++ luck/", 
"<f+++++++++ luck/wyy"
]
}

setup

目的

收集远程主机的facts

选项

过滤:-a “filter=ansible_mounts” 每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息,如操作系统版本、IP地址等报告给远程的ansbile主机;

示例

[root@ansible1 ~]#ansible web1 -m setup 
ansible1.bighug.top | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.1.250.95"
], 
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe5f:646e"
], 
"ansible_architecture": "x86_64", 
"ansible_bios_date": "05/20/2014", 
"ansible_bios_version": "6.00", 
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-327.el7.x86_64", 
"LANG": "zh_CN.UTF-8", 
"crashkernel": "auto", 
"quiet": true, 
"rd.lvm.lv": "centos/swap", 
"rhgb": true, 
"ro": true, 
"root": "/dev/mapper/centos-root"
}, 
"ansible_date_time": {
"date": "2016-09-09", 
"day": "09", 
"epoch": "1473386782", 
"hour": "10", 
"iso8601": "2016-09-09T02:06:22Z", 
"iso8601_basic": "20160909T100622991762", 
"iso8601_basic_short": "20160909T100622", 
"iso8601_micro": "2016-09-09T02:06:22.991831Z", 
"minute": "06", 
"month": "09", 
"second": "22", 
"time": "10:06:22", 
"tz": "CST", 
"tz_offset": "+0800", 
"weekday": "星期五", 
"weekday_number": "5", 
"weeknumber": "36", 
"year": "2016"
}, 
"ansible_default_ipv4": {
"address": "10.1.250.95", 
"alias": "eno16777736", 
"broadcast": "10.1.255.255", 
"gateway": "10.1.0.1", 
"interface": "eno16777736", 
"macaddress": "00:0c:29:5f:64:6e", 
"mtu": 1500, 
"netmask": "255.255.0.0", 
"network": "10.1.0.0", 
"type": "ether"
}, 
"ansible_default_ipv6": {}, 
"ansible_devices": {
"fd0": {
"holders": [], 
"host": "", 
"model": null, 
"partitions": {}, 
"removable": "1", 
"rotational": "1", 
"sas_address": null, 
"sas_device_handle": null, 
"scheduler_mode": "deadline", 
"sectors": "8", 
"sectorsize": "512", 
"size": "4.00 KB", 
"support_discard": "0", 
"vendor": null
}, 
"sda": {
"holders": [], 
"host": "", 
"model": "VMware Virtual S", 
"partitions": {
"sda1": {
"sectors": "1024000", 
"sectorsize": 512, 
"size": "500.00 MB", 
"start": "2048"
}, 
"sda2": {
"sectors": "40916992", 
"sectorsize": 512, 
"size": "19.51 GB", 
"start": "1026048"
}
}, 
"removable": "0", 
"rotational": "1", 
"sas_address": null, 
"sas_device_handle": null, 
"scheduler_mode": "deadline", 
"sectors": "41943040", 
"sectorsize": "512", 
"size": "20.00 GB", 
"support_discard": "0", 
"vendor": "VMware,"
}, 
"sr0": {
"holders": [], 
"host": "", 
"model": "VMware IDE CDR10", 
"partitions": {}, 
"removable": "1", 
"rotational": "1", 
"sas_address": null, 
"sas_device_handle": null, 
"scheduler_mode": "cfq", 
"sectors": "8456192", 
"sectorsize": "2048", 
"size": "16.13 GB", 
"support_discard": "0", 
"vendor": "NECVMWar"
}
}, 
"ansible_distribution": "CentOS", 
"ansible_distribution_major_version": "7", 
"ansible_distribution_release": "Core", 
"ansible_distribution_version": "7.2.1511", 
"ansible_dns": {
"nameservers": [
"10.1.0.1"
], 
"search": [
"bighug.top"
]
}, 
"ansible_domain": "bighug.top", 
"ansible_eno16777736": {
"active": true, 
"device": "eno16777736", 
"ipv4": {
"address": "10.1.250.95", 
"broadcast": "10.1.255.255", 
"netmask": "255.255.0.0", 
"network": "10.1.0.0"
}, 
"ipv6": [
{
"address": "fe80::20c:29ff:fe5f:646e", 
"prefix": "64", 
"scope": "link"
}
], 
"macaddress": "00:0c:29:5f:64:6e", 
"module": "e1000", 
"mtu": 1500, 
"pciid": "0000:02:01.0", 
"promisc": false, 
"type": "ether"
}, 
"ansible_env": {
"HOME": "/root", 
"LANG": "zh_CN.UTF-8", 
"LC_ALL": "zh_CN.UTF-8", 
"LC_MESSAGES": "zh_CN.UTF-8", 
"LESSOPEN": "||/usr/bin/lesspipe.sh %s", 
"LOGNAME": "root", 
"MAIL": "/var/mail/root", 
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", 
"PWD": "/root", 
"SHELL": "/bin/bash", 
"SHLVL": "2", 
"SSH_CLIENT": "10.1.250.95 44347 22", 
"SSH_CONNECTION": "10.1.250.95 44347 10.1.250.95 22", 
"SSH_TTY": "/dev/pts/2", 
"TERM": "xterm", 
"USER": "root", 
"XDG_RUNTIME_DIR": "/run/user/0", 
"XDG_SESSION_ID": "4", 
"_": "/usr/bin/python"
}, 
"ansible_fips": false, 
"ansible_form_factor": "Other", 
"ansible_fqdn": "ansible1.bighug.top", 
"ansible_gather_subset": [
"hardware", 
"network", 
"virtual"
], 
"ansible_hostname": "ansible1", 
"ansible_interfaces": [
"lo", 
"eno16777736"
], 
"ansible_kernel": "3.10.0-327.el7.x86_64", 
"ansible_lo": {
"active": true, 
"device": "lo", 
"ipv4": {
"address": "127.0.0.1", 
"broadcast": "host", 
"netmask": "255.0.0.0", 
"network": "127.0.0.0"
}, 
"ipv6": [
{
"address": "::1", 
"prefix": "128", 
"scope": "host"
}
], 
"mtu": 65536, 
"promisc": false, 
"type": "loopback"
}, 
"ansible_lvm": {
"lvs": {
"root": {
"size_g": "17.51", 
"vg": "centos"
}, 
"swap": {
"size_g": "2.00", 
"vg": "centos"
}
}, 
"vgs": {
"centos": {
"free_g": "0", 
"num_lvs": "2", 
"num_pvs": "1", 
"size_g": "19.51"
}
}
}, 
"ansible_machine": "x86_64", 
"ansible_machine_id": "6c8c524b60a842878775880168aea719", 
"ansible_memfree_mb": 3, 
"ansible_memory_mb": {
"nocache": {
"free": 37, 
"used": 184
}, 
"real": {
"free": 3, 
"total": 221, 
"used": 218
}, 
"swap": {
"cached": 0, 
"free": 2027, 
"total": 2047, 
"used": 20
}
}, 
"ansible_memtotal_mb": 221, 
"ansible_mounts": [
{
"device": "/dev/mapper/centos-root", 
"fstype": "xfs", 
"mount": "/", 
"options": "rw,relatime,attr2,inode64,noquota", 
"size_available": 17646624768, 
"size_total": 18788384768, 
"uuid": "516416a0-69a5-46f7-ab0e-fbf3d2ec1fc4"
}, 
{
"device": "/dev/sda1", 
"fstype": "xfs", 
"mount": "/boot", 
"options": "rw,relatime,attr2,inode64,noquota", 
"size_available": 390520832, 
"size_total": 520794112, 
"uuid": "b838d2bd-fd06-4443-b0b2-dec84329b8c9"
}
], 
"ansible_nodename": "ansible1.bighug.top", 
"ansible_os_family": "RedHat", 
"ansible_pkg_mgr": "yum", 
"ansible_processor": [
"GenuineIntel", 
"Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz"
], 
"ansible_processor_cores": 1, 
"ansible_processor_count": 1, 
"ansible_processor_threads_per_core": 1, 
"ansible_processor_vcpus": 1, 
"ansible_product_name": "VMware Virtual Platform", 
"ansible_product_serial": "VMware-56 4d e2 f3 84 62 d1 03-48 86 07 9b 09 5f 64 6e", 
"ansible_product_uuid": "564DE2F3-8462-D103-4886-079B095F646E", 
"ansible_product_version": "None", 
"ansible_python": {
"executable": "/usr/bin/python", 
"has_sslcontext": true, 
"type": "CPython", 
"version": {
"major": 2, 
"micro": 5, 
"minor": 7, 
"releaselevel": "final", 
"serial": 0
}, 
"version_info": [
2, 
7, 
5, 
"final", 
0
]
}, 
"ansible_python_version": "2.7.5", 
"ansible_selinux": {
"status": "disabled"
}, 
"ansible_service_mgr": "systemd", 
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDqTsQPSRoycugcW8QrS/JUZJANA5X+J2JX3Zu1xHcuABsEpTkz1Pm1DofqvXNxZFatT6DwF0JaOwMMMJU0DwIM=", 
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAID0VKaCpiZ3PiLQs0fpE5nuTLI4l2BJykdLzqG/+Lyai", 
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDSKSe9h5oJn2L+ucyU5omJTdy2Qjwd+J+9kUTZVAH4+bS/ArtvyjLBpTTeTLppRlc1YNEjffMlzJbMzwFELHKt58GvvbWhz5C7kYs95eyObHVn3BDIOIzDerI4UPhu/lg9ElkYcGtGXXPCEuqb/BRLeFHuWryO+pwSTs0F5A0sB+K/rqJCQTWz4b05kSJaqA5d3UZd3ofaNKQjbYg1w+tNUmmbhKR6Jvz4mMQl4hl+CVfdBzX4ERfnxUm4wtKHpFNPCj0UAsrjd0lgmfCyfZeKdZJrJeLHEiVj6bkAtUGw0gPpfuo35i8Pgr4iMU9eTdD5lC6Oe6/LkCtklc98+4tl", 
"ansible_swapfree_mb": 2027, 
"ansible_swaptotal_mb": 2047, 
"ansible_system": "Linux", 
"ansible_system_capabilities": [
"cap_chown", 
"cap_dac_override", 
"cap_dac_read_search", 
"cap_fowner", 
"cap_fsetid", 
"cap_kill", 
"cap_setgid", 
"cap_setuid", 
"cap_setpcap", 
"cap_linux_immutable", 
"cap_net_bind_service", 
"cap_net_broadcast", 
"cap_net_admin", 
"cap_net_raw", 
"cap_ipc_lock", 
"cap_ipc_owner", 
"cap_sys_module", 
"cap_sys_rawio", 
"cap_sys_chroot", 
"cap_sys_ptrace", 
"cap_sys_pacct", 
"cap_sys_admin", 
"cap_sys_boot", 
"cap_sys_nice", 
"cap_sys_resource", 
"cap_sys_time", 
"cap_sys_tty_config", 
"cap_mknod", 
"cap_lease", 
"cap_audit_write", 
"cap_audit_control", 
"cap_setfcap", 
"cap_mac_override", 
"cap_mac_admin", 
"cap_syslog", 
"35", 
"36+ep"
], 
"ansible_system_capabilities_enforced": "True", 
"ansible_system_vendor": "VMware, Inc.", 
"ansible_uptime_seconds": 220, 
"ansible_user_dir": "/root", 
"ansible_user_gecos": "root", 
"ansible_user_gid": 0, 
"ansible_user_id": "root", 
"ansible_user_shell": "/bin/bash", 
"ansible_user_uid": 0, 
"ansible_userspace_architecture": "x86_64", 
"ansible_userspace_bits": "64", 
"ansible_virtualization_role": "guest", 
"ansible_virtualization_type": "VMware", 
"module_setup": true
}, 
"changed": false
}

Search

    Table of Contents