用Ansible raw模块实现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/

10-12 12:30
查看更多