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

相关推荐
代码AC不AC16 小时前
【Linux】线程概念
linux·线程
神奇小梵17 小时前
关于finalshell的使用
linux·服务器·网络
vortex517 小时前
进程管理器大横评:从 PM2 到 Systemd 的选型与实战
linux·shell·进程管理
Yana.nice17 小时前
/etc/rhsm/rhsm.conf配置文件解析
linux
Jurio.17 小时前
Codex cli 分屏并行运行
linux·ai·远程工作·codex
dangdanding17 小时前
防火墙 IP 分片测试套件-fragroute
linux·网络·网络协议·tcp/ip
happymade17 小时前
全网拓扑自动发现与服务器全维度监控的技术实践
linux·运维·服务器·网络·zabbix·路由器·prometheus
米高梅狮子18 小时前
01.CentOS-Stream-8-packstack安装OpenStack
linux·云原生·容器·kubernetes·centos·自动化·openstack
xiaoye-duck18 小时前
《Linux系统编程》Linux基础开发工具 (二):详解自动化构建 make / Makefile
linux
cui_ruicheng18 小时前
Linux网络编程(五):基于UDP实现DictServer
linux·服务器·网络·udp