# YML Error when using shell module

**URL:** <https://forum.ansible.com/t/yml-error-when-using-shell-module/34583>\
**Category:** Ansible Project\
**Created:** [February 7, 2021, 3:43am UTC](https://forum.ansible.com/t/yml-error-when-using-shell-module/34583 "2021-02-07T03:43:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaptainBrewing](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@CaptainBrewing](https://forum.ansible.com/u/CaptainBrewing)\
**Post date:** [February 7, 2021, 3:43am UTC](https://forum.ansible.com/t/yml-error-when-using-shell-module/34583/1 "2021-02-07T03:43:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 7, 2021, 6:38am UTC](https://forum.ansible.com/t/yml-error-when-using-shell-module/34583/2 "2021-02-07T06:38:36Z")

</div>

> 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

Did you try one of the dedicated docker modules?

[https://docs.ansible.com/ansible/latest/collections/community/general/docker\_container\_module.html](https://docs.ansible.com/ansible/latest/collections/community/general/docker_container_module.html)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 7, 2021, 8:08am UTC](https://forum.ansible.com/t/yml-error-when-using-shell-module/34583/3 "2021-02-07T08:08:30Z")

</div>

FWIW, test the run-string first. For example

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

should give

&nbsp;&nbsp;result.stdout: 'echo "rs.initiate({\_id: \"mongors1conf\",  
&nbsp;&nbsp;configsvr: true, members: [{\_id: 0, host: \"mongocfg1\"}, {\_id: 1,  
&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_init: "{{ (my\_init\_yaml|to\_json|tojson)[1:-1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_init\_yaml:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_id: mongors1conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configsvr: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;members:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- \_id: 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: mongocfg1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- \_id: 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: mongocfg2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- \_id: 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: mongocfg3  
&nbsp;&nbsp;&nbsp;&nbsp;- shell: "echo 'echo \"rs.initiate({{ my\_init }})\"|mongo'"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout

should give valid escaped JSON

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

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

---

<div class="post-metadata">

**Author:** ![CaptainBrewing](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@CaptainBrewing](https://forum.ansible.com/u/CaptainBrewing)\
**Post date:** [February 10, 2021, 11:40pm UTC](https://forum.ansible.com/t/yml-error-when-using-shell-module/34583/4 "2021-02-10T23:40:12Z")

</div>

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
