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

相关推荐
hhhh明2 小时前
quest2+alvr+steamvr
linux·windows·quest2
dqsh063 小时前
树莓派5+Ubuntu24.04 LTS CH348 / CH9344 驱动安装 保姆级教程
linux·c语言·单片机·嵌入式硬件·iot
menge23334 小时前
Linux DNS域名解析服务器练习
linux·运维·服务器
wsad05324 小时前
CentOS 7 更换腾讯云 yum 源及 EPEL 源
linux·centos·腾讯云
wdfk_prog4 小时前
[Linux]学习笔记系列 -- [kernel]kallsyms
linux·笔记·学习
emiya_saber5 小时前
Linux 进程调度管理
linux·运维·服务器
不脱发的程序猿5 小时前
嵌入式Linux:线程同步(读写锁)
linux·嵌入式
yangzhi_emo5 小时前
配置dns主从服务
linux·运维·服务器
SongYuLong的博客7 小时前
Linux开源代码汇总
linux·运维·服务器
JiMoKuangXiangQu7 小时前
Linux 内存管理 (5):buddy 内存分配简要流程
linux·内存管理·buddy 分配器