How Do You Do Variable Expansion with an Ansible Playbook without Introducing Punctuation?

Problem scenario
You use a variable in an Ansible playbook. The value of the variable is surrounded by quotes and brackets. You may refer to it as punctuation [""]. You want to assign the variable without the new symbols such as '[""]'. How do you get just the value and no punctuation?

Solution
Do not use quotes or square brackets "[]" around the variable.

Here is an example that will provide the variable without any extra punctuation:
shell: 'echo {{ ansible_hostname }} > /tmp/date.txt'

Here is an example that will include new symbols:
shell: 'echo "{{ [ansible_hostname] }}" > /tmp/date.txt'

You may also want to see How Do You Get an Ansible Variable to Not Add Single Quotes or Brackets when the Variable Is Substituted?.

Leave a comment

Your email address will not be published. Required fields are marked *