1、在group1的被管理机里的mariadb里创建一个abc库;
1)
然后我们到agent主机上进行检查:
可以看到数据库已经创建成功。
再看几个其他命令:
#a组主机重启mysql,并设置开机自启
ansible a -m service -a "name=mysql enabled=yes state=restarted"
script模块:
script模块用于在远程主机上执行本地脚本。
脚本在master上,但是可以在agent服务器上执行。
[root@localhost ~]# cat /tmp/1.sh
#!/bin/bash
yum install -y mariadb-server &> /dev/null
systemctl start mariadb
systemctl enable mariadb
mysql << EOF
create database abc;
quit
EOF
说明:上面这个创建数据库的方法可以参考下。
[root@localhost ~]# ansible group1 -m script -a "/tmp/1.sh"
192.168.17.105 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.17.105 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.17.105 closed."
],
"stdout": "Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.\r\n",
"stdout_lines": [
"Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service."
]
}
192.168.17.106 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.17.106 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.17.106 closed."
],
"stdout": "Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.\r\n",
"stdout_lines": [
"Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service."
]
}
执行结果:
Shell能解决的,可以通过这种方式来解决的。学完Python比Shell严谨点。Shell有点天马行空的解决。