mysql - Ansible 原始和引用

标签 mysql escaping quotes ansible

使用 Ansible 原始模块实现 mysql 请求的正确方法是什么?

ansible host -m raw -a 'mysql mydb -e "SELECT lastname FROM table WHERE fistname = 'Joe'"' --su -vvvv

这不起作用并给我以下输出

<host> 
<host> PubkeyAuthentication=no ConnectTimeout=10 'su  root -c "$SHELL -c '"'"'echo SUDO-SUCCESS-sbqanpltaxkqfzxjdnpjytpnytyhrvow; mysql mydb -e "SELECT lastname FROM table WHERE firstname=Joe"'"'"'"' GSSAPIAuthentication=no User=user ControlPath=/home/user/.ansible/cp/ansible-ssh-%h-%p-%r ControlMaster=auto ControlPersist=60s
host | FAILED | rc=1 >>

value: -c: line 0: unexpected EOF while looking for matching `''
value: -c: line 1: syntax error: unexpected end of file
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
Shared connection to host closed.


ERROR 1054 (42S22) at line 1: Unknown column 'firstname' in 'where clause'

那么如果我尝试逃跑:

ansible host -m raw -a 'mysql mydb -e \"SELECT lastname FROM table WHERE fistname = 'Joe'\"'

Ansible 删除命令中的单引号,给出以下输出:

<host> 
<host> PubkeyAuthentication=no ConnectTimeout=10 GSSAPIAuthentication=no   User=user 'su  root -c "$SHELL -c '"'"'echo SUDO-SUCCESS-dmbzdeiqiygmuoebxlswxfzdaxxutgqp; mysql mydb -e \"SELECT value FROM table WHERE firstname=Joe\"'"'"'"' ControlPath=/home/user/.ansible/cp/ansible-ssh-%h-%p-%r ControlMaster=auto ControlPersist=60s
host | FAILED | rc=1 >>
ERROR 1054 (42S22) at line 1: Unknown column 'firstname' in 'where clause'
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
Shared connection to host closed.

最佳答案

根据 Ansible 文档,the raw module用于执行“低级且肮脏的 SSH 命令”,并且只应在几个非常特定的情况下使用。鉴于文档所述,您根本不应该使用此模块来尝试调用 mysql。

您可能想要做的是使用 command (或 shell )模块:

$ ansible -i ./hosts localhost -m command -a 'mysql -u user -ppass -h mydbhost -e "show databases"'

我刚刚尝试过,它按预期工作。

关于mysql - Ansible 原始和引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942581/

相关文章:

mysql - 我正在用旧的 xampp mysql 数据文件夹更新新的 xampp

mysql - 特定表自动递增 2(无 php 处理)

android - 如何将 ArrayList<String> 传递给另一个 Activity 并将其转换为 double

bash - 如何将引用的参数从变量传递给 bash 脚本

php - 应该在正则表达式中转义哪些文字字符?

javascript - JavaScript 中的多级引用?

mysql - 嵌套查询的命令不同步错误

c# - 在 SQL 查询中转义值(带有 SQL 连接器的 C#)

regex - 正则表达式中的 perl 正则表达式