ansible 通过shell脚本执行MySQL语句

1、shell 脚本:mysql.sh

#!/bin/sh
mysql -h127.0.0.1 -uroot -pRU#@xcN1NGSp etcmj  < /tmp/mysql.sql


#-u用户名 -p密码  etcmj(数据库名称)  
#mysql.sql  数据库命令

2、需要执行的数据库命令:mysql.sql

show tables;

3、ansible-playbook脚本:mysql.yml

---
- hosts: web
  remote_user: root
  tasks:
    - name: copy sh
      copy: src=/home/mysql.sh  dest=/tmp mode=755
    - name: copy sql
      copy: src=/home/mysql.sql dest=/tmp  mode=755
    - name: 执行脚本
      shell: /tmp/mysql.sh

  

原文地址:https://www.cnblogs.com/fanlong0212/p/12592552.html