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

相关推荐
小白同学_C3 小时前
Lab3-page tables && MIT6.1810操作系统工程【持续更新】
linux·c/c++·操作系统os
十年磨一剑~4 小时前
Linux程序接收到sigpipe信号崩溃处理
linux
geshifei4 小时前
Sched ext回调3——select_cpu(linux 6.15.7)
linux·ebpf
代码游侠4 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
你真是饿了4 小时前
6.库制作与原理
linux·服务器
Zach_yuan6 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
北京迅为7 小时前
《【北京迅为】itop-3568开发板NPU使用手册》- 第 7章 使用RKNN-Toolkit-lite2
linux·人工智能·嵌入式·npu
Dragon~Snow7 小时前
Linux Centos9 安装 Elasticsearch
linux·elasticsearch·jenkins
熊延7 小时前
麒麟V10系统安装部署elasticsearch
linux·运维·服务器·elasticsearch·搜索引擎·全文检索
Jia ming7 小时前
跟踪器与事件使用举例
linux·事件·跟踪器