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

lineinfile module converts double quotes to single quotes unexpectedly #40476

Closed
pjmcquade opened this issue May 21, 2018 · 3 comments
Closed
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@pjmcquade
Copy link

pjmcquade commented May 21, 2018

ISSUE TYPE
  • Bug Report
COMPONENT NAME

lineinfile

ANSIBLE VERSION
ansible 2.5.3
  config file = [REDACTED]
  configured module search path = [u'/home/a769/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
CONFIGURATION

DEFAULT_FORKS([REDACTED]) = 15
DEFAULT_HOST_LIST[REDACTED]) = [u'/data/ansible-best-practice/inventories/all.inventory']
DEFAULT_REMOTE_USER([REDACTED]) = ansnixpr
DEFAULT_STDOUT_CALLBACK([REDACTED]) = skippy
DEFAULT_TIMEOUT([REDACTED]) = 30
HOST_KEY_CHECKING([REDACTED]) = False

OS / ENVIRONMENT

RedHat 7.3 (controller and target)

SUMMARY

double quotes being converted to single quotes; escaping does not help.

STEPS TO REPRODUCE
- name: fixing katello issue bugzilla <https://bugzilla.redhat.com/show_bug.cgi?id=1356714>
  lineinfile:
    dest: /etc/rhsm/facts/katello.facts
    create: yes
    mode: 0644
    owner: root
    group: root
    line: |
      { "network.hostname" : "{{ ansible_fqdn }}" }
EXPECTED RESULTS

Expected file: /etc/rhsm/facts/katello.facts to contain:

{ "network.hostname" : "hostname.example.com" }

ACTUAL RESULTS

File contained: all double quotes converted to single quotes. Suspect text is being interpreted as JSON format and quoting is occuring. Need double quotes:

{'network.hostname': 'hostname.example.com'}

<cl50168> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansnixpr -o ConnectTimeout=30 -o ControlPath=/home/a769/.ansible/cp/1557137bb9 -tt cl50168 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-jdrutbolsrcifhskyrjvbvreedlauery; /usr/bin/python /home/ansnixpr/.ansible/tmp/ansible-tmp-1526900035.75-49518744568055/lineinfile.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<cl50168> (0, '\r\n{"msg": "line added", "diff": [{"after": "", "before_header": "/etc/rhsm/facts/katello.facts (content)", "after_header": "/etc/rhsm/facts/katello.facts (content)", "before": ""}, {"before_header": "/etc/rhsm/facts/katello.facts (file attributes)", "after_header": "/etc/rhsm/facts/katello.facts (file attributes)"}], "changed": true, "backup": "", "invocation": {"module_args": {"directory_mode": null, "force": null, "remote_src": null, "backrefs": false, "insertafter": null, "path": "/etc/rhsm/facts/katello.facts", "owner": "root", "follow": false, "validate": null, "group": "root", "insertbefore": null, "unsafe_writes": null, "create": true, "setype": null, "content": null, "serole": null, "state": "present", "dest": "/etc/rhsm/facts/katello.facts", "selevel": null, "regexp": null, "line": "{\'network.hostname\': \'cl50168.sunlifecorp.com\'}", "src": null, "seuser": null, "delimiter": null, "mode": 420, "firstmatch": false, "attributes": null, "backup": false}}}\r\n', 'OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data /home/a769/.ssh/config\r\ndebug1: /home/a769/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 3: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 56878\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\nShared connection to cl50168 closed.\r\n')
<cl50168> ESTABLISH SSH CONNECTION FOR USER: ansnixpr
<cl50168> SSH: ansible.cfg set ssh_args: (-C)(-o)(ControlMaster=auto)(-o)(ControlPersist=60s)
<cl50168> SSH: ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled: (-o)(StrictHostKeyChecking=no)
<cl50168> SSH: ansible_password/ansible_ssh_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)
<cl50168> SSH: ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set: (-o)(User=ansnixpr)
<cl50168> SSH: ANSIBLE_TIMEOUT/timeout set: (-o)(ConnectTimeout=30)
<cl50168> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/a769/.ansible/cp/1557137bb9)
<cl50168> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansnixpr -o ConnectTimeout=30 -o ControlPath=/home/a769/.ansible/cp/1557137bb9 cl50168 '/bin/sh -c '"'"'rm -f -r /home/ansnixpr/.ansible/tmp/ansible-tmp-1526900035.75-49518744568055/ > /dev/null 2>&1 && sleep 0'"'"''
<cl50168> (0, '', 'OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013\r\ndebug1: Reading configuration data /home/a769/.ssh/config\r\ndebug1: /home/a769/.ssh/config line 1: Applying options for *\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 3: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 56878\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 0\r\n')
changed: [cl50168] => {
    "backup": "",
    "changed": true,
    "diff": [
        {
            "after": "",
            "after_header": "/etc/rhsm/facts/katello.facts (content)",
            "before": "",
            "before_header": "/etc/rhsm/facts/katello.facts (content)"
        },
        {
            "after_header": "/etc/rhsm/facts/katello.facts (file attributes)",
            "before_header": "/etc/rhsm/facts/katello.facts (file attributes)"
        }
    ],
    "invocation": {
        "module_args": {
            "attributes": null,
            "backrefs": false,
            "backup": false,
            "content": null,
            "create": true,
            "delimiter": null,
            "dest": "/etc/rhsm/facts/katello.facts",
            "directory_mode": null,
            "firstmatch": false,
            "follow": false,
            "force": null,
            "group": "root",
            "insertafter": null,
            "insertbefore": null,
            "line": "{'network.hostname': 'cl50168.sunlifecorp.com'}",
            "mode": 420,
            "owner": "root",
            "path": "/etc/rhsm/facts/katello.facts",
            "regexp": null,
            "remote_src": null,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "state": "present",
            "unsafe_writes": null,
            "validate": null
        }
    },
    "msg": "line added"
}

Note: removing the opening and closing braces. I.e.:

Change:
{ "network.hostname" : "{{ ansible_fqdn }}" }
to:
"network.hostname" : "hostname.example.com"

yields the expected output in the file (i.e. the double quotes are not converted to single quotes).

@pjmcquade pjmcquade changed the title lineinfile module converts double quotes to sibgle quotes unexpectedly lineinfile module converts double quotes to single quotes unexpectedly May 21, 2018
@ansibot
Copy link
Contributor

ansibot commented May 21, 2018

Files identified in the description:

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

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented May 21, 2018

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels May 21, 2018
@sivel
Copy link
Member

sivel commented May 21, 2018

We are aware of this issue. This is not a side affect of the lineinfile module munging data, but a complex interaction between ansible and jinja2 (used for templating).

The problem is that your string contains a template, and because it looks like a python dictionary, it gets converted to a python dictionary, and loses it's context as a string.

There is a more in depth explanation available at #34595 (comment)

This will hopefully be resolved once we can implement jinja2 native types (#32738)

If you have further questions please stop by IRC or the mailing list:

@sivel sivel closed this as completed May 21, 2018
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label May 21, 2018
@ansible ansible locked and limited conversation to collaborators May 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. 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