[root@promote ~]# ansible web -m service -a 'name=httpd enabled=true state=stopped'
192.168.199.130 | CHANGED => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "stopped",
"status": {
[root@web ~]# systemctl status httpd.service #再次到web服务器进行查看
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 一 2018-10-22 23:54:30 CST; 25s ago #可以看到httpd已经关闭
10 shell 模块
ansible中的shell模块可以在被管理主机上运行命令,并支持像管道符号等功能的复杂命令。
[root@promote ~]# 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 set
to no/false.
下面我创建一个Jerry用户,并为这个用户设置密码:
[root@promote ~]# ansible web -m user -a 'name=jerry' #创建Jerry用户
192.168.199.130 | CHANGED => {
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/jerry",
"name": "jerry",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
[root@promote ~]# ansible web -m shell -a 'echo 123456 | passwd --stdin jerry' #为用户设置密码为123456
192.168.199.130 | CHANGED | rc=0 >>
更改用户 jerry 的密码 。
passwd:所有的身份验证令牌已经成功更新。
11 script 模块
ansible中的script模块可以将本地脚本复制到被管理主机上进行运行。需要注意的是,使用相对路径来指定脚本。