第3章 Ansible中的模块说明
[root@m01 ~]# ansible all -m ping
172.16.1.8 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.41 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.31 | SUCCESS => {
"changed": false,
"ping": "pong"
}
连接正常返回 pong 通过帮助信息可以获得 ↓
通过 ansible-doc -v ping 可以获得该模块的说明
ansible-doc -s file 参看模块的具体信息
[root@m01 ~]# ansible-doc -v ping
Using /etc/ansible/ansible.cfg as config file
> PING (/usr/lib/python2.6/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.
不指定模块的时候默认使用的模块就是command ↓
[root@m01 ~]# ansible all -a "date"
172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017
172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017
172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:15 CST 2017
使用ansible自带模块执行命令 如果要用 > < | & ‘ ‘ 使用shell模块
[root@m01 ~]# ansible all -m command -a "date"
172.16.1.8 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017
172.16.1.31 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:28 CST 2017
172.16.1.41 | SUCCESS | rc=0 >>
Thu Oct 19 17:12:27 CST 2017
chdir参数的使用:
[root@m01 ~]# ansible linuxidc -m command -a "chdir=/tmp pwd"
172.16.1.31 | SUCCESS | rc=0 >>
/tmp
172.16.1.8 | SUCCESS | rc=0 >>
/tmp
172.16.1.41 | SUCCESS | rc=0 >>
/tmp
creates 文件是否存在,不存在就执行命令
[root@m01 ~]# ansible linuxidc -m command -a "creates=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
skipped, since /etc/hosts exists
removes 文件是否存在,不存在就不执行命令,
[root@m01 ~]# ansible linuxidc -m command -a "removes=/etc/hosts date"
172.16.1.31 | SUCCESS | rc=0 >>
Fri Oct 20 13:32:40 CST 2017
执行linux命令时可以用
远程节点执行命令
说明: shell 模块在远程执行脚本时,远程主机上一定要有相应的脚本
[root@m01 ~]# ansible linuxidc -m shell -a "/bin/sh /server/scripts/ssh-key.sh"
172.16.1.31 | SUCCESS | rc=0 >>
fenfa 172.16.1.31 [ OK ]
fenfa 172.16.1.41 [ OK ]
fenfa 172.16.1.8 [ OK ]
在本地执行脚本时,将脚本中的内容传输到远程节点上运行
[root@m01 ~]# ansible all -m script -a "/server/scripts/free.sh"
172.16.1.8 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.16.1.8 closed.\r\n",
"stdout": " total used free shared buffers cached\r\nMem: 474M 377M 97M 532K 54M 202M\r\n-/+ buffers/cache: 120M 354M\r\nSwap: 767M 0B 767M\r\n",
"stdout_lines": [
" total used free shared buffers cached",
"Mem: 474M 377M 97M 532K 54M 202M",
"-/+ buffers/cache: 120M 354M",
"Swap: 767M 0B 767M"
]
}
说明:
使用scripts模块,不用将脚本传输到远程节点,脚本本身不用进行授权,即可利用script模块执行。直接执行脚本即可,不需要使用sh
说明: src和content不能同时使用
使用copy 模块,将/etc/hosts 文件 传输到各个服务器送,权限修改为0600 属主属组为linuxidc
[root@m01 ~]# ansible linuxidc -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=linuxidc group=oldboy "
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
"dest": "/tmp/hosts",
"gid": 500,
"group": "linuxidc",
"md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
"mode": "0600",
"owner": "linuxidc",
"size": 357,
"src": "/root/.ansible/tmp/ansible-tmp-1508410846.63-224022812989166/source",
"state": "file",
"uid": 500
}
……
检查结果
[root@m01 ~]# ansible all -m shell -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 19 19:00 /tmp/hosts
172.16.1.41 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 11 15:12 /tmp/hosts
172.16.1.8 | SUCCESS | rc=0 >>
-rw------- 1 linuxidc oldboy 357 Oct 19 19:00 /tmp/hosts
移动远程主机上的文件 remote_src=true 参数
[root@m01 ~]# ansible linuxidc -m copy -a " src=/server/scripts/ssh-key.sh dest=/tmp/ remote_src=true"
172.16.1.41 | SUCCESS => {
"changed": true,
"checksum": "d27bd683bd37e15992d2493b50c9410e0f667c9c",
"dest": "/tmp/ssh-key.sh",
"gid": 0,
"group": "root",
"md5sum": "dc88a3a419e3657bae7d3ef31925cbde",
"mode": "0644",
"owner": "root",
"size": 397,
"src": "/server/scripts/ssh-key.sh",
"state": "file",
"uid": 0
}
定义文件中的内容 content=linuxidcedu.com 默认没有换行
[root@m01 ~]# ansible linuxidc -m copy -a "content=linuxidcedu.com dest=/tmp/linuxidc666.txt"
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum": "291694840cd9f9c464263ea9b13421d8e74b7d00",
"dest": "/tmp/linuxidc666.txt",
"gid": 0,
"group": "root",
"md5sum": "0a6bb40847793839366d0ac014616d69",
"mode": "0644",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1508466752.1-24733562369639/source",
"state": "file",
"uid": 0
}
注意:重命名和创建多级目录不能同时实现
创建目录
[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=directory"
172.16.1.41 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/linuxidc_dir",
"size": 4096,
"state": "directory",
"uid": 0
}
创建文件
[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=touch"
172.16.1.8 | SUCCESS => {
"changed": true,
"dest": "/tmp/linuxidc_file",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
创建软连接
[root@m01 ~]# ansible linuxidc -m file -a "src=/tmp/linuxidc_file dest=/tmp/linuxidc_file_link state=link"
172.16.1.41 | SUCCESS => {
"changed": true,
"dest": "/tmp/linuxidc_file_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 16,
"src": "/tmp/linuxidc_file",
"state": "link",
"uid": 0
}
删除目录文件信息
[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_dir state=absent"
172.16.1.41 | SUCCESS => {
"changed": true,
"path": "/tmp/linuxidc_dir",
"state": "absent"
[root@m01 ~]# ansible linuxidc -m file -a "dest=/tmp/linuxidc_file state=absent"
172.16.1.31 | SUCCESS => {
"changed": true,
"path": "/tmp/linuxidc_file",
"state": "absent"
创建多级目录
[root@m01 ~]# ansible linuxidc -m copy -a "src=/etc/hosts dest=/tmp/01/0/0/0/0/0/0/0/"
172.16.1.31 | SUCCESS => {
"changed": true,
"checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
"dest": "/tmp/01/0/0/0/0/0/0/0/hosts",
"gid": 0,
"group": "root",
"md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
"mode": "0644",
"owner": "root",
"size": 357,
"src": "/root/.ansible/tmp/ansible-tmp-1508466973.39-99676412390473/source",
"state": "file",
"uid": 0
}
注意:重命名和创建多级目录不能同时实现
从远程拉取出来文件
[root@m01 cp]# ansible linuxidc -m fetch -a "dest=/tmp/backup src=/etc/hosts"
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
"dest": "/tmp/backup/172.16.1.8/etc/hosts",
"md5sum": "8c2b120b4742a806dcfdc8cfff6b6308",
"remote_checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
"remote_md5sum": null
}
[root@m01 cp]# tree /tmp/backup/
/tmp/backup/
├── 172.16.1.31
│ └── etc
│ └── hosts
├── 172.16.1.41
│ └── etc
│ └── hosts
└── 172.16.1.8
└── etc
└── hosts
flat 参数,拉去的时候不创建目录(同名文件会覆盖)
[root@m01 tmp]# ansible linuxidc -m fetch -a "dest=/tmp/backup/ src=/etc/hosts flat=yes"
172.16.1.8 | SUCCESS => {
"changed": false,
"checksum": "b3c1ab140a1265cd7f6de9175a962988d93c629b",
"dest": "/tmp/backup/hosts",
"file": "/etc/hosts",
"md5sum": "8c2b120b4742a806dcfdc8cfff6b6308"
挂载
[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/ src=172.16.1.31:/data/ state=mounted"
172.16.1.8 | SUCCESS => {
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/mnt/",
"opts": "rw",
"passno": "0",
"src": "172.16.1.31:/data/"
}
卸载
[root@m01 tmp]# ansible 172.16.1.8 -m mount -a "fstype=nfs opts=rw path=/mnt/ src=172.16.1.31:/data/ state=unmounted"
172.16.1.8 | SUCCESS => {
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/mnt/",
"opts": "rw",
"passno": "0",
"src": "172.16.1.31:/data/"
}
添加定时任务
[root@m01 ~]# ansible linuxidc -m cron -a "minute=0 hour=0 name=linuxidc01"
172.16.1.8 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"linuxidc01"
]
}
删除定时任务
[root@m01 ~]# ansible linuxidc -m cron -a "minute=00 hour=00 name=linuxidc01 state=absent"
172.16.1.8 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
只用名字就可以删除
[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 state=absent"
172.16.1.31 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
注释定时任务
注意: 注释定时任务的时候必须有job的参数
[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 disabled=yes"
172.16.1.31 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"linuxidc01"
]
}
取消注释
[root@m01 ~]# ansible linuxidc -m cron -a "name=linuxidc01 disabled=no"
172.16.1.41 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"linuxidc01"
]
}
[root@m01 ~]# ansible linuxidc -m yum -a "name=nmap state=installed "
172.16.1.31 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, security\nSetting up Install Process\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * epel: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:5.51-6.el6 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:5.51-6.el6 base 2.8 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\n\nTotal download size: 2.8 M\nInstalled size: 9.7 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : 2:nmap-5.51-6.el6.x86_64 1/1 \n\r Verifying : 2:nmap-5.51-6.el6.x86_64 1/1 \n\nInstalled:\n nmap.x86_64 2:5.51-6.el6 \n\nComplete!\n"
]
}
说明 :service 管理的服务必须存在在/etc/init.d/下有的服务脚本
重启定时任务
[root@m01 ~]# ansible linuxidc -m service -a "name=crond state=restarted"
172.16.1.8 | SUCCESS => {
"changed": true,
"name": "crond",
"state": "started"
}
[root@m01 ~]# ansible 172.16.1.8 -m hostname -a "name=web01"
172.16.1.8 | SUCCESS => {
"ansible_facts": {
"ansible_domain": "etiantian.org",
"ansible_fqdn": "www.etiantian.org",
"ansible_hostname": "web01",
"ansible_nodename": "web01"
},
"changed": false,
"name": "web01"
}
[root@m01 ~]# ansible 172.16.1.8 -m selinux -a "state=disabled"
172.16.1.8 | SUCCESS => {
"changed": false,
"configfile": "/etc/selinux/config",
"msg": "",
"policy": "targeted",
"state": "disabled"
}
[root@m01 ~]# ansible 172.16.1.8 -m get_url -a "url=http://lan.linuxidc.com/RDPWrap-v1.6.1.zip dest=/tmp/"
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "ad402705624d06a6ff4b5a6a98c55fc2453b3a70",
"dest": "/tmp/RDPWrap-v1.6.1.zip",
"gid": 0,
"group": "root",
"md5sum": "b04dde546293ade71287071d187ed92d",
"mode": "0644",
"msg": "OK (1567232 bytes)",
"owner": "root",
"size": 1567232,
"src": "/tmp/tmp4X4Von",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://lan.linuxidc.com/RDPWrap-v1.6.1.zip"
}
url= 下载文件的地址 dest 下载到哪里
timeout 超时时间
url_password 密码
url_username 用户名