共计 5053 个字符,预计需要花费 13 分钟才能阅读完成。

使用ad-hoc方式记录下一些基础模块使用
命令模块
command
注意:Ad-hoc默认使用该模块command(不支持管道重定向等符号)
[root@manager ~]# ansible nfs -m command -a 'w' | |
nfs01 | CHANGED | rc=0 >> | |
02:51:03 up 2:22, 2 users, load average: 0.00, 0.01, 0.05 | |
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT | |
root pts/0 10.0.0.1 00:29 2:00m 0.21s 0.21s -bash | |
root pts/1 172.16.1.61 02:51 0.00s 0.12s 0.00s w |
shell
注意:比command高级,可支持管道重定向等符号,但遇到$会被解析
[root@manager ~] | |
nfs01 | CHANGED | rc=0 >> | |
10.0.0.31 172.16.1.31 192.168.154.155 | |
[root@manager ~] | |
nfs01 | CHANGED | rc=0 >> | |
10.0.0.31 |
用户和组模块
user
创建不可登录用户
[root@manager ~] | |
nfs01 | CHANGED => { | |
"changed": true, | |
"comment": "", | |
"create_home": false, | |
"group": 666, | |
"home": "/home/aaa", | |
"name": "aaa", | |
"shell": "/sbin/nologin", | |
"state": "present", | |
"system": false, | |
"uid": 888 | |
} |
创建可登录用户
[root@manager ~] | |
nfs01 | CHANGED => { | |
"changed": true, | |
"comment": "", | |
"create_home": true, | |
"group": 1001, | |
"home": "/home/bbb", | |
"name": "bbb", | |
"shell": "/bin/sh", | |
"state": "present", | |
"system": false, | |
"uid": 1001 | |
} |
生成用户密码
# 生成密码 | |
[root@manager ~]# ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}" | |
localhost | SUCCESS => { | |
"msg": "$6$mysecretsalt$qJbapG68nyRab3gxvKWPUcs2g3t0oMHSHMnSKecYNpSi3CuZm.GbBqXO8BE6EI6P1JUefhA0qvD7b5LSh./PU1" | |
} |
设置用户密码
#修改用户密码 | |
[root~]# ansible nfs -m user -a 'name=bbb password=$6$mysercretsalt$qF7c7b4aO14EVnPghilcTrsA8tDwGM54aeIDs1ljo3B1yPHDPQKhGddHoPTe.YARL.RBsrmSzgqR3LxgSfuCs0' | |
nfs01 | CHANGED => { | |
"append": false, | |
"changed": true, | |
"comment": "", | |
"group": 1001, | |
"home": "/home/bbb", | |
"move_home": false, | |
"name": "bbb", | |
"password": "NOT_LOGGING_PASSWORD", | |
"shell": "/bin/sh", | |
"state": "present", | |
"uid": 1001 | |
} |
group
创建用户组
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"gid": 1002, | |
"name": "hhh", | |
"state": "present", | |
"system": false | |
} |
删除用户组
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"name": "hhh", | |
"state": "absent" | |
} |
文件操作模块
file
创建文件
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"dest": "/tmp/test", | |
"gid": 0, | |
"group": "root", | |
"mode": "0600", | |
"owner": "root", | |
"size": 0, | |
"state": "file", | |
"uid": 0 | |
} |
创建目录
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"gid": 0, | |
"group": "root", | |
"mode": "0755", | |
"owner": "root", | |
"path": "/tmp/mydir", | |
"size": 6, | |
"state": "directory", | |
"uid": 0 | |
} |
copy
复制主控端文件到客户端节点上
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"checksum": "bc221e76ab69607d5f5c3153cbd0f2b277b6093a", | |
"dest": "/tmp/hosts", | |
"gid": 0, | |
"group": "root", | |
"md5sum": "bc61c74b4cd4253907a6e142b40bc65b", | |
"mode": "0600", | |
"owner": "root", | |
"size": 347, | |
"src": "/root/.ansible/tmp/ansible-tmp-1545162295.34-119407730773856/source", | |
"state": "file", | |
"uid": 0 | |
} |
content
在客户端节点创建文件并生成指定内容
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"checksum": "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", | |
"dest": "/tmp/gg", | |
"gid": 0, | |
"group": "root", | |
"md5sum": "5d41402abc4b2a76b9719d911017c592", | |
"mode": "0600", | |
"owner": "root", | |
"size": 5, | |
"src": "/root/.ansible/tmp/ansible-tmp-1545162706.77-72798555539608/source", | |
"state": "file", | |
"uid": 0 | |
} |
服务操作模块
service&systemd
开启服务
[ | ]|
[ | ]
停止服务
[ | ]|
[ | ]
重启服务
[ | ]|
[ | ]
systemd服务配置reload
[ | ]|
[ | ]
mount
[root@manager ~] | |
[root@manager ~] | |
[root@manager ~] | |
[root@manager ~] |
cron
注意:创建定时同步时间任务,名字必须指定,ansible是根据名字来处理定时任务的增删改的
创建定时任务
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"envs": [], | |
"jobs": [ | |
"sync time" | |
] | |
} | |
[ | ]|
00 00 * * 1 ntpdate ntp.api.bz &>/dev/null |
删除定时任务
[ | ]|
nfs01 | CHANGED => { | |
"changed": true, | |
"envs": [], | |
"jobs": [] | |
} |
yum
[ | ]|
nfs01 | SUCCESS => { | |
"ansible_facts": { | |
"pkg_mgr": "yum" | |
}, | |
"changed": false, | |
"msg": "", | |
"rc": 0, | |
"results": [ | |
"1:nfs-utils-1.3.0-0.61.el7.x86_64 providing nfs-utils is already installed" | |
] | |
} |
正文完