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


On Sun, Feb 7, 2021 at 3:08 AM Vladimir Botka <[email protected]> wrote:

> On Sat, 6 Feb 2021 19:43:07 -0800 (PST)
> CaptainBrewing <[email protected]> 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

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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL_P1G8P0YAF_UZijM3SSaVws03b9uTdg4njPDw5fEoE%3DpByYw%40mail.gmail.com.

Reply via email to