1、shell模块
功能说明:在远程节点上执行一个命令,且支持特殊符号< > | &等
[root@m01~]# ansible-doc -s shell
- name: Execute commands in nodes.
shell:
chdir: # cd into this directory before running the command
creates: # a filename, when it already exists, this step will
*not* be run.
executable: # change the shell used to execute the command.
Should be an
absolute path to the
executable.
free_form: # (required) The shell module takes a free form
command to run, as a
string. There's not
an actual option
named "free form".
See the examples!
removes: # a filename, when it does not exist, this step will
*not* be run.
stdin: # Set the stdin of the command directly to the
specified value.
warn: # if command warnings are on in ansible.cfg, do not
warn about this
particular line if
实践:增加文本
[root@m01~]# ansible oldboy -m shell -a "echo oldboy >/tmp/tmp.txt"
172.16.1.31 | CHANGED | rc=0 >>
172.16.1.41 | CHANGED | rc=0 >>
[root@m01~]# ansible oldboy -m shell -a "cat /tmp/tmp.txt"
172.16.1.41 | CHANGED | rc=0 >>
oldboy
172.16.1.31 | CHANGED | rc=0 >>
Oldboy
注意:要执行的脚本必须在远端机器上存在。
root@m01/server/scripts]# ansible oldboy -m shell -a "sh /server/scripts/bak.sh"
172.16.1.31 | CHANGED | rc=0 >>
172.16.1.41 | CHANGED | rc=0 >>
2、copy模块
功能:复制文件到远程主机
参数说明:
实践:
1、把/etc/hosts拷贝到/opt下,权限设置400,用户和组设置root
[root@m01~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/opt owner=root group=root mode=400"
172.16.1.41 | CHANGED => {
"changed": true,
"checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "fa2c31e4614da1256984a0307ba4f31f",
"mode": "0400",
"owner": "root",
"size": 332,
"src": "/root/.ansible/tmp/ansible-tmp-1556093513.66-247733471881082/source",
"state": "file",
"uid": 0
}
172.16.1.31 | CHANGED => {
"changed": true,
"checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "fa2c31e4614da1256984a0307ba4f31f",
"mode": "0400",
"owner": "root",
"size": 332,
"src": "/root/.ansible/tmp/ansible-tmp-1556093513.65-270555927258619/source",
"state": "file",
"uid": 0
}
结果:
[root@m01/]# ansible oldboy_pass -m command -a "ls /opt"
172.16.1.40 | CHANGED | rc=0 >>
hosts
172.16.1.30 | CHANGED | rc=0 >>
hosts
2、把/etc/passwd拷贝/tmp下改名为oldgirl,用户和组为oldboy,权限600,如果有存在同名文件覆盖
[root@m01/]# ansible oldboy_pass -m copy -a "src=/etc/passwd dest=/tmp/oldgirl owner=oldboy group=oldboy mode=600"
172.16.1.30 | CHANGED => {
"changed": true,
"checksum": "1263a7c5c95b15b4ea35435fbae126a424f37b4c",
"dest": "/tmp/oldgirl",
"gid": 1000,
"group": "oldboy",
"md5sum": "031f4ce8d77faf23cf5c4df9a7adeec5",
"mode": "0600",
"owner": "oldboy",
"size": 1050,
"src": "/root/.ansible/tmp/ansible-tmp-1556094254.84-3643956437803/source",
"state": "file",
"uid": 1000
}
172.16.1.40 | CHANGED => {
"changed": true,
"checksum": "1263a7c5c95b15b4ea35435fbae126a424f37b4c",
"dest": "/tmp/oldgirl",
"gid": 1000,
"group": "oldboy",
"md5sum": "031f4ce8d77faf23cf5c4df9a7adeec5",
"mode": "0600",
"owner": "oldboy",
"size": 1050,
"src": "/root/.ansible/tmp/ansible-tmp-1556094254.84-274426832795456/source",
"state": "file",
"uid": 1000
}
结果:
[root@m01/]# ansible oldboy_pass -m command -a "ls /tmp"
172.16.1.40 | CHANGED | rc=0 >>
ansible_command_payload_sK0b3M
oldgirl
172.16.1.30 | CHANGED | rc=0 >>
ansible_command_payload_gDliN9
oldgirl
3、script模块
功能说明:远程节点上执行本地脚本
4、file模块
功能参数:设置文件属性
参数:
创建软链接:
[root@m01/]# ansible oldboy -m file -a "src=/data/line.sh dest=/data/line.sh_link state=link"
172.16.1.40 | CHANGED => {
"changed": true,
"dest": "/data/line.sh_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 13,
"src": "/data/line.sh",
"state": "link",
"uid": 0
}
172.16.1.30 | CHANGED => {
"changed": true,
"dest": "/data/line.sh_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 13,
"src": "/data/line.sh",
"state": "link",
"uid": 0
}
结果:
[root@backup/data]# ll /data
总用量 0
-rw-r--r-- 1 root root 0 4月 24 17:03 line.sh
lrwxrwxrwx 1 root root 13 4月 24 17:03 line.sh_link -> /data/line.sh
创建目录:mkdir /tmp/oldboy_dir
ansible oldboy -m file -a “dest=/tmp/oldboy_dir state=directory”
递归设置权限:
ansible oldboy -m file -a “dest=/tmp/oldboy_dir state= directory mode=644 recurse=yes”
创建文件:touch /tmp/oldboy_dir
ansible oldboy -m file -a “dest=/tmp/oldboy_dir state=absent”
创建链接文件:ln -s /etc/hosts /tmp/link_file
ansible oldboy -m file -a “src=/etc/hosts dest=/tmp/link_file state=link”
5、yum模块
功能说明:yum包管理模块
6、service模块(C6)
功能说明:管理服务模块
实践:
[root@m01/]# ansible oldboy -m systemd -a "name=crond.service state=stopped enabled=no"
[root@m01/]# ansible oldboy -m systemd -a "name=crond state=started enabled=yes"
[root@m01/]# ansible oldboy -m command -a "systemctl status crond"
8、cron模块
功能说明:管理定时任务条目信息模块
参数查找 ansible-doc -s cron
定时任务参数:
minute: # Minute when the job should run ( 0-59, *, */2, etc )
hour: # Hour when the job should run ( 0-23, *, */2, etc )
day: # Day of the month the job should run ( 1-31, *, */2, etc )
month: # Month of the year the job should run ( 1-12, *, */2, etc )
weekday: # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
job: # The command to execute or, if env is set, the value of environment variable. The command should not contain line breaks. Required if
state=present.
创建定时任务:
[root@m01/]# ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
172.16.1.40 | CHANGED => {
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
172.16.1.30 | CHANGED => {
"changed": true,
"envs": [],
"jobs": [
"sync time"
]
}
结果:
[root@m01/]# ansible oldboy -m command -a "crontab -l"
172.16.1.30 | CHANGED | rc=0 >>
#crond-id-001:time sync by oldboy
*/5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1
#Ansible: sync time
00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
172.16.1.40 | CHANGED | rc=0 >>
#crond-id-001:time sync by oldboy
*/5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com >/dev/null 2>&1
#Ansible: sync time
00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1