逃离Elixir 1.4中的字符串

时间:2017-04-18 21:56:37

标签: erlang elixir

我知道我可能在这里遗漏了一些东西

我有这个红宝石代码

system("ansible all -i #{ip_address}, -m lineinfile -u root -a'dest=/etc/setup.json state=present regexp=rpc_json line='\\\''\"rpc_json\": \"#{ip_address}:2012\",'\\\'")

我正在尝试使用System.cmd

重现这一点
System.cmd("ansible" ,["all", "-i", "127.0.0.1,","-m", "lineinfile","-u","root","-a","dest=/etc/cgrates/cgrates.json state=present regexp=rpc_json 
line='\\\''\"rpc_json\": \"#{ip_address}:2012\",'\\\'" ])

问题在于:

 line='\\\''\"rpc_json\": \"#{ip_address}:2012\",'\\\'" 

试图绕过rpc_json的双引号和ip_address的内插值

尝试使用~s sigil无济于事。任何指向我错误的指针都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用erlang的:os.cmd/1命令?只需要记住发送一个charlist而不是elixir字符串。

iex(9)> ip_address = "127.0.0.1"
"127.0.0.1"
iex(10)> cmd = "ansible all -i #{ip_address}, -m lineinfile -u root -a'dest=/etc/setup.json state=present regexp=rpc_json line='\\\''\"rpc_json\": \"#{ip_address}:2012\",'\\\'"
"ansible all -i 127.0.0.1, -m lineinfile -u root -a'dest=/etc/setup.json state=present regexp=rpc_json line='\\''\"rpc_json\": \"127.0.0.1:2012\",'\\'"
iex(11)> :os.cmd String.to_charlist(cmd)
'127.0.0.1 | UNREACHABLE! => {\n    "changed": false, \n    "msg": "Failed to connect to the host via ssh: ssh_askpass: exec(/usr/X11R6/bin/ssh-askpass): No such file or directory\\r\\nHost key verification failed.\\r\\n", \n    "unreachable": true\n}\n'