YML Error when using shell module

31 views
Skip to first unread message

CaptainBrewing

unread,
Feb 6, 2021, 10:43:07 PM2/6/21
to Ansible Project
Hello, I am using the shell module to run a command inside a docker container and it is giving me a yaml error. The command is stupid silly complicated ;-) Is there a way to get Ansible to just send it as is and ignore \ escape out the characters that may be freaking out Yaml?

Here is an example of a  basic command that does work. 

shell: docker exec -it  9d5e563a6e9e  bash -c "cd /var/log ; ls; cat bootstrap.log"


However this command gives me a yaml eror 

shell: docker exec -it 9d5e563a6e9e  bash -c "echo 'rs.initiate({_id: \"mongors1conf\",configsvr: true, members: [{ _id : 0, host : \"mongocfg1\" },{ _id : 1, host : \"mongocfg2\" }, { _id : 2, host : \"mongocfg3\" }]})' | mongo"


Here is a snippet of the error 
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

    shell: docker exec -it 9d5e563a6e9e  bash -c "echo 'rs.initiate({_id: \"mongors1conf\",configsvr: true, members: [{ _id : 0, host : \"mongocfg1\" },{ _id : 1, host : \"mongocfg2\" }, { _id : 2, host : \"mongocfg3\" }]})' | mongo"
                                                                                                ^ here


This command works just fine at the bash shell of the docker host. I have tried the ansible shell and the Ansible command module 

I know this command looks complicated but please keep in mind I am only asking how to format this to work in Ansible. I am not asking you to understand docker exec Mongodb commands ;-) 

Thanks for your help 



Dick Visser

unread,
Feb 7, 2021, 1:38:57 AM2/7/21
to ansible...@googlegroups.com
On Sun, 7 Feb 2021 at 04:43, CaptainBrewing <lan...@gmail.com> wrote:
Hello, I am using the shell module to run a command inside a docker container and it is giving me a yaml error. The command is stupid silly complicated ;-) Is there a way to get Ansible to just send it as is and ignore \ escape out the characters that may be freaking out Yaml?

Here is an example of a  basic command that does work. 

shell: docker exec -it  9d5e563a6e9e  bash -c "cd /var/log ; ls; cat bootstrap.log"


However this command gives me a yaml eror 

shell: docker exec -it 9d5e563a6e9e  bash -c "echo 'rs.initiate({_id: \"mongors1conf\",configsvr: true, members: [{ _id : 0, host : \"mongocfg1\" },{ _id : 1, host : \"mongocfg2\" }, { _id : 2, host : \"mongocfg3\" }]})' | mongo"


Here is a snippet of the error 
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

    shell: docker exec -it 9d5e563a6e9e  bash -c "echo 'rs.initiate({_id: \"mongors1conf\",configsvr: true, members: [{ _id : 0, host : \"mongocfg1\" },{ _id : 1, host : \"mongocfg2\" }, { _id : 2, host : \"mongocfg3\" }]})' | mongo"
                                                                                                ^ here


This command works just fine at the bash shell of the docker host. I have tried the ansible shell and the Ansible command module 


I know this command looks complicated but please keep in mind I am only asking how to format this to work in Ansible. I am not asking you to understand docker exec Mongodb commands ;-) 

Thanks for your help 



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a8cf8352-2e86-4ca8-9179-2a1600b26a89n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Vladimir Botka

unread,
Feb 7, 2021, 3:08:51 AM2/7/21
to CaptainBrewing, ansible...@googlegroups.com
On Sat, 6 Feb 2021 19:43:07 -0800 (PST)
CaptainBrewing <lan...@gmail.com> wrote:

> Hello, I am using the shell module to run a command inside a docker
> container and it is giving me a yaml error. The command is stupid silly
> complicated ;-) Is there a way to get Ansible to just send it as is and
> ignore \ escape out the characters that may be freaking out Yaml?
>
> Here is an example of a basic command that does work.
>
> *shell*: docker exec -it 9d5e563a6e9e bash -c "cd /var/log ; ls; cat
> bootstrap.log"
>
>
> However this command gives me a yaml eror
>
> *shell*: docker exec -it 9d5e563a6e9e bash -c "echo 'rs.initiate({_id:
> \"mongors1conf\",configsvr: true, members: [{ _id : 0, host : \"mongocfg1\"
> },{ _id : 1, host : \"mongocfg2\" }, { _id : 2, host : \"mongocfg3\" }]})'
> | mongo"

FWIW, test the run-string first. For example

- set_fact:
my_init: '{_id: \"mongors1conf\",
configsvr: true,
members: [{_id: 0, host: \"mongocfg1\"},
{_id: 1, host: \"mongocfg2\"},
{_id: 2, host: \"mongocfg3\"}]}'
- shell: "echo 'echo \"rs.initiate({{ my_init }})\"|mongo'"
register: result
- debug:
var: result.stdout

should give

result.stdout: 'echo "rs.initiate({_id: \"mongors1conf\",
configsvr: true, members: [{_id: 0, host: \"mongocfg1\"}, {_id: 1,
host: \"mongocfg2\"}, {_id: 2, host: \"mongocfg3\"}]})"|mongo'

Send it to mongo if it looks like what you want. But, mongo should
accept JSON. It'd be simpler to create a dictionary in YAML and use
the Ansible filter *to_json* to convert the data. In addition to this
filter use Jinja filter *tojson* to escape the quotes and strip the
leading and cloding quote. For example

- set_fact:
my_init: "{{ (my_init_yaml|to_json|tojson)[1:-1] }}"
vars:
my_init_yaml:
_id: mongors1conf
configsvr: true
members:
- _id: 0
host: mongocfg1
- _id: 1
host: mongocfg2
- _id: 2
host: mongocfg3
- shell: "echo 'echo \"rs.initiate({{ my_init }})\"|mongo'"
register: result
- debug:
var: result.stdout

should give valid escaped JSON

result.stdout: 'echo "rs.initiate({\"_id\": \"mongors1conf\",
\"configsvr\": true, \"members\": [{\"_id\": 0, \"host\":
\"mongocfg1\"}, {\"_id\": 1, \"host\": \"mongocfg2\"}, {\"_id\": 2,
\"host\": \"mongocfg3\"}]})"|mongo'

Ref: No quotes in MongoDB JSON
https://stackoverflow.com/questions/35100836/no-quotes-in-mongodb-json


--
Vladimir Botka

Joe Langdon

unread,
Feb 10, 2021, 6:40:36 PM2/10/21
to Vladimir Botka, ansible...@googlegroups.com
Yeah I did look at the Docker Module. Unfortunately, it does not have any modules for what I am doing

Vladimir, very thankful for your well thought out and detailed answer! This gives me enough information to explore the json option

--

Joe Langdon

Sometimes when you think life is kicking you in the ass, it's actually just moving you quickly to a better place.


Reply all
Reply to author
Forward
0 new messages