Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double Quotes in Command arguments are replaced with single quotes #71665

Closed
tsia opened this issue Sep 7, 2020 · 3 comments
Closed

Double Quotes in Command arguments are replaced with single quotes #71665

tsia opened this issue Sep 7, 2020 · 3 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. commands Commands category module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@tsia
Copy link

tsia commented Sep 7, 2020

SUMMARY
  • I have some JSON data in a Variable (result of a previous command)
  • looking at the output of debug the JSON content is valid
  • i'm using this variable as a argument to another command
  • the double quotes inside this variable are replaced with single quotes causing tools like jq to complain because that is not valid JSON
ISSUE TYPE
  • Bug Report
COMPONENT NAME

command

ANSIBLE VERSION
ansible 2.9.12
  config file = None
  configured module search path = ['/home/<redacted>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.5/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.5.3 (default, Jul  9 2020, 13:00:10) [GCC 6.3.0 20170516]
CONFIGURATION
<empty>
OS / ENVIRONMENT

target OS: CentOS 8
Python 3.6.8

STEPS TO REPRODUCE
---
- name: Test
  hosts: all
  become: yes
  tasks:
    - name: get some json data
      command:
        argv:
          - jq
          - .auths
          - /tmp/registry-auth.json
      register: docker_auth_data
    - debug:
        var: docker_auth_data
    - name: merge pull secrets
      command:
        argv:
          - jq
          - -c
          - --argjson
          - var
          - "{{ docker_auth_data.stdout }}"
          - .auths += $var
          - /tmp/pull-secret.json
EXPECTED RESULTS

json inside a variable should stay valid JSON

ACTUAL RESULTS
$ ansible-playbook -i hosts test.yml

PLAY [Test] ********************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [ansible.local]

TASK [get some json data] ******************************************************************************************************************************************************************
changed: [ansible.local]

TASK [debug] *******************************************************************************************************************************************************************************
ok: [ansible.local] => {
    "docker_auth_data": {
        "changed": true,
        "cmd": [
            "jq",
            ".auths",
            "/tmp/registry-auth.json"
        ],
        "delta": "0:00:00.007783",
        "end": "2020-09-07 16:42:30.771254",
        "failed": false,
        "rc": 0,
        "start": "2020-09-07 16:42:30.763471",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "{\n  \"registry.example.com:8443\": {\n    \"auth\": \"Zm9vYmFyOm15X3NlY3JldF9wYXNzd29yZA==\"\n  }\n}",
        "stdout_lines": [
            "{",
            "  \"registry.example.com:8443\": {",
            "    \"auth\": \"Zm9vYmFyOm15X3NlY3JldF9wYXNzd29yZA==\"",
            "  }",
            "}"
        ]
    }
}

TASK [merge pull secrets] ******************************************************************************************************************************************************************
fatal: [ansible.local]: FAILED! => {"changed": true, "cmd": ["jq", "-c", "--argjson", "var", "{'registry.example.com:8443': {'auth': 'Zm9vYmFyOm15X3NlY3JldF9wYXNzd29yZA=='}}", ".auths += $var", "pull-secret.json"], "delta": "0:00:00.004928", "end": "2020-09-07 16:42:31.626875", "msg": "non-zero return code", "rc": 2, "start": "2020-09-07 16:42:31.621947", "stderr": "jq: invalid JSON text passed to --argjson\nUse jq --help for help with command-line options,\nor see the jq manpage, or online docs  at https://stedolan.github.io/jq", "stderr_lines": ["jq: invalid JSON text passed to --argjson", "Use jq --help for help with command-line options,", "or see the jq manpage, or online docs  at https://stedolan.github.io/jq"], "stdout": "", "stdout_lines": []}

PLAY RECAP *********************************************************************************************************************************************************************************
ansible.local  : ok=3    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

output of run with -vvv: https://gist.github.com/tsia/34ebab74c71a20caef5eb5ca4ff648e8

@ansibot
Copy link
Contributor

ansibot commented Sep 7, 2020

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. commands Commands category module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Sep 7, 2020
@bcoca
Copy link
Member

bcoca commented Sep 8, 2020

You will need - "{{ docker_auth_data.stdout|string }}" or to_json to prevent/force conversions back into JSON as they automatically get converted internally into Python data structures, which when serialized get single quotes, why you are seeing that 'substitution' happen but it is not really about quotes but data formats.

@bcoca bcoca closed this as completed Sep 8, 2020
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Sep 8, 2020
@tsia
Copy link
Author

tsia commented Sep 8, 2020

Thanks for clarifying. So that means I should consider all stdout of command as "potentially an object" just to be on the safe side?

@ansible ansible locked and limited conversation to collaborators Oct 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. commands Commands category module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

3 participants