Ansible学习笔记10

1、在group1的被管理机里的mariadb里创建一个abc库;

1)

然后我们到agent主机上进行检查:

可以看到数据库已经创建成功。

再看几个其他命令:

bash 复制代码
#a组主机重启mysql,并设置开机自启
ansible a -m service -a "name=mysql enabled=yes state=restarted"

script模块:

script模块用于在远程主机上执行本地脚本。

脚本在master上,但是可以在agent服务器上执行。

bash 复制代码
[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

说明:上面这个创建数据库的方法可以参考下。

bash 复制代码
[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有点天马行空的解决。

相关推荐
Uncertainty!!1 小时前
Linux多用户情况下个别用户输入密码后黑屏
linux·远程连接
necessary6531 小时前
使用Clion查看linux环境中的PG源码
linux·运维·服务器
小猪佩奇TONY3 小时前
Linux 内核学习(14) --- linux x86-32 虚拟地址空间
linux·学习
Lam㊣3 小时前
Centos 7 系统docker:更换镜像源
linux·docker·centos
FL16238631293 小时前
win11+WSL+Ubuntu-xrdp+远程桌面闪退+黑屏闪退解决
linux·运维·ubuntu
石头5303 小时前
Kubernetes监控全栈解决方案:从零搭建Prometheus+Grafana监控体系
linux
ha20428941943 小时前
Linux操作系统学习记录之---TcpSocket
linux·网络·c++·学习
AOwhisky4 小时前
Linux逻辑卷管理:从“固定隔间”到“弹性存储池”的智慧
linux·运维·服务器
C Yu小白5 小时前
Linux系统调用与文件操作详解
linux·运维·服务器
ZFB00015 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——常见用户组简介
linux·运维·kylin