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

Quotes style exception: single quotes don't work for escape characters #8

Open
SiqingYu opened this issue Mar 4, 2021 · 0 comments

Comments

@SiqingYu
Copy link

SiqingYu commented Mar 4, 2021

As https://github.com/whitecloud/ansible-styleguide#quotes stated, single quotes should be preferred for strings; double quotes are only allowed if embedded in Jinja templates. But it seems that I found an exception from my work where only double quotes work, but single quotes do not.

# test.yml
---

- name: test special quote cases
  hosts: localhost
  vars:
    tabbed_strings:
      - "aaa\t111"
      - "bbb\t222"
      - "ccc\t333"
  tasks:
    - name: double quotes work
      debug:
        msg: "{{ item.split('\t')[0] }}" 
      loop: "{{ tabbed_strings }}"

    - name: single quotes don't work
      debug:
        msg: '{{ item.split("\t")[0] }}'
      loop: '{{ tabbed_strings }}'

Output:

➜ ansible-playbook test.yml -v
Using /home/sqyu/code/greatdb-ansible/ansible.cfg as config file

PLAY [test special quote cases] ***********************************************************************************************************************************************************************************

TASK [double quotes work] *****************************************************************************************************************************************************************************************
ok: [localhost] => (item=aaa    111) => 
  msg: aaa
ok: [localhost] => (item=bbb    222) => 
  msg: bbb
ok: [localhost] => (item=ccc    333) => 
  msg: ccc

TASK [single quotes don't work] ***********************************************************************************************************************************************************************************
ok: [localhost] => (item=aaa    111) => 
  msg: "aaa\t111"
ok: [localhost] => (item=bbb    222) => 
  msg: "bbb\t222"
ok: [localhost] => (item=ccc    333) => 
  msg: "ccc\t333"

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

The output shows that the split() function doesn't work for single-quoted strings.

Why do I use double quotes for tabbed_strings in the above case? Because I found it is how Ansible's register produces strings from stdout.

The official Ansible YAML syntax documentation stated that single quotes don't work with escape characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant