Escaping Strings in Elixir 1.4 - erlang

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.

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'

Related

Docker failed to load listeners, cannot assign requested address

I'm using this guide to try and run up Docker using WSL2. I've got everything starting however there is an issue when I actually try to run up Docker. Once I run the command sudo dockerd -H `ifconfig eth0 | grep -E "([0-9]{1,3}.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d:
WARN[2022-02-01T11:07:40.033323500-06:00] Binding to IP address without --tlsverify is insecure and gives root access on this machine to everyone who has access to your network. host="tcp://169.254.77.26:2375"
WARN[2022-02-01T11:07:40.033991800-06:00] Binding to an IP address, even on localhost, can also give access to scripts run in a browser. Be safe out there! host="tcp://169.254.77.26:2375"
WARN[2022-02-01T11:07:41.036303800-06:00] Binding to an IP address without --tlsverify is deprecated. Startup is intentionally being slowed down to show this message host="tcp://169.254.77.26:2375"
WARN[2022-02-01T11:07:41.043536700-06:00] Please consider generating tls certificates with client validation to prevent exposing unauthenticated root access to your network host="tcp://169.254.77.26:2375"
WARN[2022-02-01T11:07:41.044564400-06:00] You can override this by explicitly specifying '--tls=false' or '--tlsverify=false' host="tcp://169.254.77.26:2375"
WARN[2022-02-01T11:07:41.045654100-06:00] Support for listening on TCP without authentication or explicit intent to run without authentication will be removed in the next release host="tcp://169.254.77.26:2375"
failed to load listeners: listen tcp 169.254.77.26:2375: bind: cannot assign requested address
I'm not too familiar with Docker so not sure what I can adjust to make it launch properly. Any suggestions are appreciated, thanks!
I'm doing exactly the same.
What worked for me was this comment https://dev.to/nelsonpena/comment/1jmkb . But it was not too explicit
I opened windows PowerShell and used the command
wsl --set-version Ubuntu 2
if you have another distro of linux it would be
wsl --set-version <distroname> 2
I closed wsl and opened it again. and executed the command
echo `ifconfig eth0 | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2;exit }' | cut -f2 -d:`
and got API listen on [the IP]

Getting template error while templating string when trying to work with json and json_query filter

I try to execute this ansible command :
ansible localhost -m debug -a "var={{ db_config| to_json |json_query(*)}}" \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
To get all json elements using json_query(*)
but I'm getting the following error :
fatal: [localhost]: FAILED! =>
msg: 'template error while templating string: expected token ''end of print statement'', got ''name''. String: {{"[{\\"name\\":\\"mydb1\\",\\"services\\":[\\"app1\\",\\"app2\\"]},{\\"name\\":\\"mydb12\\",\\"services\\":[\\"app1\\"]}]"}}'
There are several issues in this single one liner:
The parameter to use for debug in your case is msg (to output the result of a template or static value) and not var (used to debug a single var without any transformation). See the debug module documentation
You are passing as a parameter a json string representation. The filter you want to use is from_json (to transform your string to a dict/list) and not to_json (which does the exact opposite).
Your jmespath expression in json_query is invalid for two reason:
It's not a string (just a bare wildcard caracter)
Even quoted it would not respect the specification (follow the above link).
The following fixed one liner gives the result (I believe...) you expect:
$ ansible localhost -m debug -a 'msg={{ db_config | from_json | json_query("[]") }}' \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
localhost | SUCCESS => {
"msg": [
{
"name": "mydb1",
"services": [
"app1",
"app2"
]
},
{
"name": "mydb12",
"services": [
"app1"
]
}
]
}
Note that you can even drop the json decoding step by passing your arguement as a full inlined json:
$ ansible localhost -m debug -a 'msg={{ db_config | json_query("[]") }}' \
-e '{"db_config":[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]}'
localhost | SUCCESS => {
"msg": [
{
"name": "mydb1",
"services": [
"app1",
"app2"
]
},
{
"name": "mydb12",
"services": [
"app1"
]
}
]
}
I suspect you are trying all this to later test json_query expressions to select your data.
You should be aware there a lot of other core filters in ansible (among which selectattr, rejectattr, select, reject, map, dict2items, items2dict, first, last, unique, zip, subelements,...) that combined together can do most of the job json_query can do without its overhead (installing a python module and an ansible collection). json_query is only really needed for very complex queries.
In case I am wrong and your final goal is really to display all elements of your input data, you can overly simplify. From the two examples above:
$ ansible localhost -m debug -a 'msg={{ db_config | from_json }}' \
-e 'db_config=[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]'
# OR (the next one can work with `var` for debug)
$ ansible localhost -m debug -a 'var=db_config' \
-e '{"db_config":[{"name":"mydb1","services":["app1","app2"]},{"name":"mydb12","services":["app1"]}]}'
If you put the data into a file
shell> cat test-data.yml
---
db_config:
- {name: "mydb1", services: ["app1", "app2"]}
- {name: "mydb12", services: ["app1"]}
and if you put the code into the playbook
shell> cat test.yml
---
- hosts: localhost
tasks:
- debug:
msg: "{{ db_config|json_query('[]') }}"
The command works as expected
shell> ansible-playbook test.yml -e #test-data.yml
...
msg:
- name: mydb1
services:
- app1
- app2
- name: mydb12
services:
- app1

ansible over docker hosts does not as expected

I'm facing a bit weird issue targeting in ansible docker containers.
Inventory
el7_02 ansible_port=6000 ansible_user=user ansible_host=localhost
el7_03 ansible_port=6001 ansible_user=user ansible_host=localhost
playbook
- shell: hostname
register: x
- debug: msg="{{ x.stdout}}, {{ansible_hostname}}, {{ansible_user}}, {{ansible_port}}"
output
TASK [Gathering Facts] *************************************************************************************************
ok: [el7_03]
ok: [el7_02]
TASK [x : shell] *************************************************************************************************
changed: [el7_03]
changed: [el7_02]
TASK [x : debug] *************************************************************************************************
ok: [el7_03] => {
"msg": "el7_02, el7_02, user, 6001"
}
ok: [el7_02] => {
"msg": "el7_02, el7_02, user, 6000"
}
as you can see for some reasons I see not expected hostname for the container el7_03. While I'd expect to see in a debug tasks for the docker el7_03 the same hostname (i.e. el7_03 but not el7_02). Why I receive "the wrong" output?
checking hostnames in docker
~/ $ ssh -p 6000 user#localhost 'hostname'
el7_02
~/ $ ssh -p 6001 user#localhost 'hostname'
el7_03
if I will switch to ansible_connection=docker then I get what I expect. however, I cannot use it because when I interact with anything located outside of my laptop (installing anything or downloading from the internet) time to time (and quite often) I receive timeouts. Maybe there is a way how to get rid of timeouts?
os: macos
ansible: 2.9.11
python: 3.8.5
docker: 19.0.3.8
thank you
you need to work around the issue that ansible looks up a host via hostname and not via hostname:port pair .
my workaround for this issue is as follows:
$ grep pi. /etc/hosts
127.0.0.1 pi1
127.0.0.1 pi2
127.0.0.1 pi3
# inventory contents:
$ cat all_rpis.ini
pi1:3321
pi2:3322
pi3:3323

Ansible ping fails when I am not using root priviledges

I get the following error when I try to ping another docker container I setup as a remote:
"changed": false,
"msg": "Failed to connect to the host via ssh: bind: File name too long\r\nunix_listener: cannot bind to path: /var/jenkins_home/.ansible/cp/jenkins_remote-22-remote_user.15sibyvAohxbTCvh",
"unreachable": true
}
However, when I run the same command using the root user, it works.
I have tried to add add the following command to my ansible.cfg file, but it still fails.
control_path = %(directory)s/%%h-%%p-%%r
Please what could be the issue?
I had the same issue it worked with root user and printed the same error otherwise. What did help was to add the following:
[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
control_path = /dev/shm/cp%%h-%%p-%%r
to /etc/ansible/ansible.cfg file (create it if it doesn't exist).

Lua popen with string arguments

I am attempting to use popen to pipe a string containing multiple quotes to netcat. I have a Python command that works fine but I am turning it into an nmap script. I am not as familiar with Lua.
Python version:
python -c 'print "\x1b%-12345X#PJL FSDIRLIST NAME=\"0:\\..\\..\\..\\\" ENTRY=1 COUNT=999999\x0d\x0a\x1b%-12345X\x0d\x0a"' | nc 192.168.0.1 9100
Lua attempted version:
local handle = assert(io.popen("python -c 'print \"\x1b%-12345X#PJL FSDIRLIST NAME=\"0:\\..\\..\\..\\\" ENTRY=1 COUNT=999999\x0d\x0a\x1b%-12345X\x0d\x0a\"' | nc " .. host .. " " .. port, "r"))
This results in the following error:
File "<string>", line 1
print "2345X#PJL FSDIRLIST NAME="0:\..\..\..\" ENTRY=1 COUNT=999999
Is there a way to organize that string so that Lua will accept it?
Try using a long string
[[python -c 'print "\x1b%-12345X#PJL FSDIRLIST NAME=\"0:\\..\\..\\..\\\" ENTRY=1 COUNT=999999\x0d\x0a\x1b%-12345X\x0d\x0a"' | nc 192.168.0.1 9100]]

Resources