dev
dev

Reputation: 392

Escaping Strings in Elixir 1.4

I know i'm probably missing something here

I've got this ruby code

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\",'\\\'")

I'm trying to reproduce this with 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\",'\\\'" ])

Issue is with this :

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

Trying to escape double quotes around rpc_json and the interpolated value of ip_address

Tried using the ~s sigil to no avail.Any Pointer to my error would be appreciated.

Upvotes: 1

Views: 575

Answers (1)

Steve Pallen
Steve Pallen

Reputation: 4507

Have you tried using erlang's :os.cmd/1 command? Just need to remember to send it a charlist and not an elixir string.

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'

Upvotes: 1

Related Questions