Ansible 常用模块详细介绍

常用模块(module_name):
ping 模块:测试连通性
[root@ansible-master ~]# ansible all -m ping
192.168.23.73 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
连接正常返回 ping 通过帮助信息可以获得 ↓
通过 ansible-doc -v ping 可以获得该模块的说明
ansible-doc -s file 参看模块的具体信息
[root@ansible-master ~]# ansible-doc -v ping
Using /etc/ansible/ansible.cfg as config file
> PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong’ on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible’
to verify the ability to login and that a usable Python is configured. This is NOT ICMP ping, this is just a trivial test module that requires Python on the
remote-node. For Windows targets, use the [win_ping] module instead. For Network targets, use the [net_ping] module instead.
command 模块: 默认模块
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
不指定模块的时候默认使用的模块就是command
[root@ansible-master ~]# ansible all -a “date”
192.168.23.73 | SUCCESS | rc=0 >>
Sat May 5 13:37:22 CST 2018
使用ansible自带模块执行命令 如果要用 > < | & ‘ ‘ 使用shell 模块, command是不支持管道符之类的。
[root@ansible-master ~]# ansible all -m command -a “date”
192.168.23.73 | SUCCESS | rc=0 >>
Sat May 5 13:38:59 CST 2018
chdir 参数的使用:
[root@ansible-master ~]# ansible mysql -m command -a “chdir=/tmp pwd”
192.168.23.73 | SUCCESS | rc=0 >>
/tmp
creates 文件是否存在,不存在就执行命令
[root@ansible-master ~]# ansible mysql -m command -a “creates=/etc/hosts date”
192.168.23.73 | SUCCESS | rc=0 >>
skipped, since /etc/hosts exists
removes 文件是否存在,不存在就不执行命令
[root@ansible-master ~]# ansible mysql -m command -a “removes=/tmp/files date”
192.168.23.73 | SUCCESS | rc=0 >>
skipped, since /tmp/files does not exist
shell 模块:万能模块
执行linux命令时可以用
远程节点执行命令
说明: shell 模块在远程执行脚本时,远程主机上一定要有相应的脚本
[root@ansible-master ~]# ansible mysql -m shell -a “/bin/sh /server/scripts/ssh-key.sh”
192.168.23.73 | SUCCESS | rc=0 >> fenfa 192.168.23.73 [ OK ]
script 模块:执行脚本模块
执行本地脚本,将脚本的输出结果输出到远程节点上,例如执行一个脚本输出的结果是把时间写入到tmp目录下的一个txt文档中,执行后,远程的机器/tmp/目录下面就会有个txt的文档!!
[root@ansible-master ~]# ansible all -m script -a “/server/scripts/free.sh”
192.168.23.73 | SUCCESS => {
“changed”: true,
“rc”: 0,
“stderr”: “Shared connection to 192.168.23.73 closed.\r\n”,
“stdout”: “Filesystem Size Used Avail Use% Mounted on\r\n/dev/mapper/centos_moban-root 18G 2.7G 14G 17% /\r\ndevtmpfs 1.9G 0 1.9G 0% /dev\r\ntmpfs 1.9G 0 1.9G 0% /dev/shm\r\ntmpfs 1.9G 8.6M 1.9G 1% /run\r\ntmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup\r\n/dev/sda1 477M 74M 375M 17% /boot\r\n”,
“stdout_lines”: [
“Filesystem Size Used Avail Use% Mounted on”,
“/dev/mapper/centos_moban-root 18G 2.7G 14G 17% /”,
“devtmpfs 1.9G 0 1.9G 0% /dev”,
“tmpfs 1.9G 0 1.9G 0% /dev/shm”,
“tmpfs 1.9G 8.6M 1.9G 1% /run”,
“tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup”,
“/dev/sda1 477M 74M 375M 17% /boot”
]
}
说明:
使用scripts模块,不用将脚本传输到远程节点,脚本本身不用进行授权,即可利用script模块执行。直接执行脚本即可,不需要使用sh
copy模块:把本地文件发送到远端
常用参数:
Ansible 常用模块详细介绍Ansible 常用模块详细介绍
说明: src和content不能同时使用
copy常用命令参数测试
使用copy 模块,将/etc/hosts 文件 传输到各个服务器送,权限修改为0600 属主属组为root
[root@ansible-master ~]# ansible mysql -m copy -a “src=/etc/hosts dest=/tmp/ mode=0600 owner=root group=root”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “53191228bb475e07617814e38d81701feb62f6ea”,
“dest”: “/tmp/hosts”,
“gid”: 0,
“group”: “root”,
“md5sum”: “fc7e1addff573a5052ea751a96b9a5aa”,
“mode”: “0600”,
“owner”: “root”,
“size”: 180,
“src”: “/root/.ansible/tmp/ansible-tmp-1525501097.59-84548318782643/source”,
“state”: “file”,
“uid”: 0
}
检查结果
[root@ansible-master ~]# ansible all -m shell -a “ls -l /tmp/hosts”
192.168.23.73 | SUCCESS | rc=0 >>
-rw——- 1 root root 180 May 5 14:03 /tmp/hosts
移动远程主机上的文件 remote_src=true 参数
[root@ansible-master ~]# ansible mysql -m copy -a “src=/server/scripts/free.sh dest=/tmp/ remote_src=true”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “c989bd551bfa8c755f6cacacb90c5c509432110e”,
“dest”: “/tmp/free.sh”,
“gid”: 0,
“group”: “root”,
“md5sum”: “69a238d8cb3c5f979252010b3299e524”,
“mode”: “0644”,
“owner”: “root”,
“size”: 6,
“src”: “/server/scripts/free.sh”,
“state”: “file”,
“uid”: 0
}
定义文件中的内容 content=okay686.cn 默认没有换行
[root@ansible-master ~]# ansible mysql -m copy -a “content=okay686.cn dest=/tmp/okay686.txt”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “e93871bed93b47bf5a869db239e99396bde3bfaf”,
“dest”: “/tmp/okay686.txt”,
“gid”: 0,
“group”: “root”,
“md5sum”: “f110176529a2dc0dde29ff557ef7a9db”,
“mode”: “0644”,
“owner”: “root”,
“size”: 10,
“src”: “/root/.ansible/tmp/ansible-tmp-1525501820.98-171323403083294/source”,
“state”: “file”,
“uid”: 0
}
file模块: 设置文件属性
常用参数:
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
注意:重命名和创建多级目录不能同时实现
创建目录
[root@ansible-master ~]# ansible mysql -m file -a “dest=/tmp/yh_dir state=directory”
192.168.23.73 | SUCCESS => {
“changed”: true,
“gid”: 0,
“group”: “root”,
“mode”: “0755”,
“owner”: “root”,
“path”: “/tmp/yh_dir”,
“size”: 4096,
“state”: “directory”,
“uid”: 0
}
创建文件
[root@ansible-master ~]# ansible mysql -m file -a “dest=/tmp/yh_file state=touch”
192.168.23.73 | SUCCESS => {
“changed”: true,
“dest”: “/tmp/yh_file”,
“gid”: 0,
“group”: “root”,
“mode”: “0644”,
“owner”: “root”,
“size”: 0,
“state”: “file”,
“uid”: 0
}
创建软连接
[root@ansible-master ~]# ansible mysql -m file -a “src=/tmp/yh_file dest=/tmp/yh_file_link state=link”
192.168.23.73 | SUCCESS => {
“changed”: true,
“dest”: “/tmp/yh_file_link”,
“gid”: 0,
“group”: “root”,
“mode”: “0777”,
“owner”: “root”,
“size”: 12,
“src”: “/tmp/yh_file”,
“state”: “link”,
“uid”: 0
}
删除目录文件信息
[root@ansible-master ~]# ansible mysql -m file -a “dest=/tmp/yh_file state=absent”
192.168.23.73 | SUCCESS => {
“changed”: true,
“path”: “/tmp/yh_file”,
“state”: “absent”
}
创建多级目录(当然不要忘记shell)
[root@ansible-master ~]# ansible mysql -m copy -a “src=/etc/hosts dest=/tmp/01/0/0/0/0/0/0/0/”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “53191228bb475e07617814e38d81701feb62f6ea”,
“dest”: “/tmp/01/0/0/0/0/0/0/0/hosts”,
“gid”: 0,
“group”: “root”,
“md5sum”: “fc7e1addff573a5052ea751a96b9a5aa”,
“mode”: “0644”,
“owner”: “root”,
“size”: 180,
“src”: “/root/.ansible/tmp/ansible-tmp-1525502406.11-84339390216299/source”,
“state”: “file”,
“uid”: 0
}
注意:重命名和创建多级目录不能同时实现
fetch 模块:拉取文件
常用参数:
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
从远程拉取出来文件
[root@ansible-master ~]# ansible mysql -m fetch -a “dest=/tmp/backup src=/tmp/backup”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0”,
“dest”: “/tmp/backup/192.168.23.73/tmp/backup”,
“md5sum”: “ba1f2511fc30423bdbb183fe33f3dd0f”,
“remote_checksum”: “a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0”,
“remote_md5sum”: null
}
flat 参数,拉取的时候不创建目录(同名文件会覆盖)
[root@ansible-master tmp]# ansible mysql -m fetch -a “dest=/tmp/backup src=/tmp/backup falt=yes”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum”: “a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0”,
“dest”: “/tmp/backup/192.168.23.73/tmp/backup”,
“md5sum”: “ba1f2511fc30423bdbb183fe33f3dd0f”,
“remote_checksum”: “a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0”,
“remote_md5sum”: null
}
mount模块:配置挂载点模块
常用参数:
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
挂载
[root@m01 tmp]# ansible 192.168.23.73 -m mount -a “fstype=nfs opts=rw path=/mnt/ src=192.168.23.62:/data/ state=mounted” 192.168.23.73 | SUCCESS => { “changed”: true, “dump”: “0”, “fstab”: “/etc/fstab”, “fstype”: “nfs”, “name”: “/mnt/”, “opts”: “rw”, “passno”: “0”, “src”: “192.168.23.62:/data/” }
卸载
[root@m01 tmp]# ansible 192.168.23.73 -m mount -a “fstype=nfs opts=rw path=/mnt/ src=192.168.23.62:/data/ state=unmounted” 192.168.23.73| SUCCESS => { “changed”: true, “dump”: “0”, “fstab”: “/etc/fstab”, “fstype”: “nfs”, “name”: “/mnt/”, “opts”: “rw”, “passno”: “0”, “src”: “192.168.23.62:/data/” }
cron模块:定时任务
常用参数:
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
添加定时任务
[root@ansible-master ~]# ansible mysql -m cron -a “minute=0 hour=0 job=’/bin/sh /server/scripts/hostname.sh &> /dev/null’ name=yh”
192.168.23.73 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“yh”
]
}
删除定时任务
只用名字就可以删除
[root@ansible-master ~]# ansible mysql -m cron -a “name=yh state=absent”
192.168.23.73 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: []
}
注释定时任务
注意: 注释定时任务的时候必须有job的参数
[root@ansible-master ~]# ansible mysql -m cron -a “name=yh job=’/bin/sh /server/scripts/hostname.sh &> /dev/null’ disabled=yes”
192.168.23.73 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“yh”
]
}
取消注释
[root@ansible-master ~]# ansible mysql -m cron -a “name=yh job=’/bin/sh /server/scripts/hostname.sh &> /dev/null’ disabled=no”
192.168.23.73 | SUCCESS => {
“changed”: true,
“envs”: [],
“jobs”: [
“yh”
]
}
yum 模块
常用参数:
Ansible 常用模块详细介绍Ansible 常用模块详细介绍
[root@ansible-master ~]# ansible mysql -m yum -a “name=nmap state=installed”
192.168.23.73 | SUCCESS => {
“changed”: true,
“msg”: “”,
“rc”: 0,
“results”: [
“Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.cn99.com\n * extras: mirrors.163.com\n * updates: mirrors.163.com\nResolving Dependencies\n–> Running transaction check\n—> Package nmap.x86_64 2:6.40-7.el7 will be installed\n–> Processing Dependency: nmap-ncat = 2:6.40-7.el7 for package: 2:nmap-6.40-7.el7.x86_64\n–> Running transaction check\n—> Package nmap-ncat.x86_64 2:6.40-7.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n nmap x86_64 2:6.40-7.el7 base 4.0 M\nInstalling for dependencies:\n nmap-ncat x86_64 2:6.40-7.el7 base 201 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+1 Dependent package)\n\nTotal download size: 4.2 M\nInstalled size: 17 M\nDownloading packages:\n——————————————————————————–\nTotal 2.0 MB/s | 4.2 MB 00:02 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 2:nmap-ncat-6.40-7.el7.x86_64 1/2 \n Installing : 2:nmap-6.40-7.el7.x86_64 2/2 \n Verifying : 2:nmap-ncat-6.40-7.el7.x86_64 1/2 \n Verifying : 2:nmap-6.40-7.el7.x86_64 2/2 \n\nInstalled:\n nmap.x86_64 2:6.40-7.el7 \n\nDependency Installed:\n nmap-ncat.x86_64 2:6.40-7.el7 \n\nComplete!\n”
]
}
service模块:服务管理
常用参数:
Ansible 常用模块详细介绍
Ansible 常用模块详细介绍
说明 :service 管理的服务必须存在在/etc/init.d/下有的服务脚本
重启定时任务
[root@ansible-master ~]# ansible mysql -m service -a “name=crond state=restarted”
192.168.23.73 | SUCCESS => {
“changed”: true,
“name”: “crond”,
“state”: “started”,
其他模块补充
hostname:修改主机名模块
[root@ansible-master ~]# ansible mysql -m hostname -a “name=ansible-mysql”
192.168.23.73 | SUCCESS => {
“ansible_facts”: {
“ansible_domain”: “”,
“ansible_fqdn”: “ansible-mysql”,
“ansible_hostname”: “ansible-mysql”,
“ansible_nodename”: “ansible-mysql”
},
“changed”: true,
“name”: “ansible-mysql”
}
selinux:管理模块
[root@ansible-master ~]# ansible mysql -m selinux -a “state=disabled”
192.168.23.73 | SUCCESS => {
“changed”: false,
“configfile”: “/etc/selinux/config”,
“msg”: “”,
“policy”: “targeted”,
“reboot_required”: false,
“state”: “disabled”
}
get_url 模块 == 【wget】
[root@ansible-master ~]# ansible mysql -m get_url -a “url=http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz dest=/tmp/”
192.168.23.73 | SUCCESS => {
“changed”: true,
“checksum_dest”: null,
“checksum_src”: “e6e3779487182c94efd3206751927ce722c1da5c”,
“dest”: “/tmp/ngx_cache_purge-2.1.tar.gz”,
“gid”: 0,
“group”: “root”,
“md5sum”: “b403e963108f4e1700607cbe40916807”,
“mode”: “0644”,
“msg”: “OK (10535 bytes)”,
“owner”: “root”,
“size”: 10535,
“src”: “/tmp/tmpkkb8BF”,
“state”: “file”,
“status_code”: 200,
“uid”: 0,
“url”: “http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz”
}
url= 下载文件的地址 dest= 下载到哪里 timeout 超时时间 url_password 密码 url_username 用户名
总结:
Ansible 常用模块详细介绍

本文版权归 飞翔沫沫情 作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接 如有问题, 可发送邮件咨询,转贴请注明出处:https://www.fxkjnj.com/195/

发表评论

登录后才能评论