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

-- 
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/20210207090830.18d4c963%40gmail.com.

Attachment: pgp3N4etytaIZ.pgp
Description: OpenPGP digital signature

Reply via email to