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有点天马行空的解决。

相关推荐
生信大表哥6 小时前
单细胞测序分析(五)降维聚类&数据整合
linux·python·聚类·数信院生信服务器
“αβ”6 小时前
MySQL表的操作
linux·网络·数据库·c++·网络协议·mysql·https
神秘的土鸡7 小时前
openEuler 25.09 企业级 MySQL主从复制部署与性能优化实战提升50%
linux·数据库·mysql·性能优化·openeuler
知识分享小能手7 小时前
CentOS Stream 9入门学习教程,从入门到精通, CentOS Stream 9 命令行基础 —语法知识点与实战详解(4)
linux·学习·centos
烤鱼骑不快8 小时前
linux 软Raid
linux
hhwyqwqhhwy9 小时前
Linux file->private
linux·运维·服务器
WongKyunban9 小时前
在Linux下制作软件安装包
linux·运维·服务器
IT 乔峰9 小时前
Centos7中部署DNS服务器
linux
hweiyu009 小时前
Linux 命令:parted
linux·运维·服务器
搞全栈小苏9 小时前
Linux下 cmake所有版本适用/下载编译使用教程 附cmake3.26安装包
linux·运维·ubuntu