Skip to content

Instantly share code, notes, and snippets.

@pfac
Last active November 20, 2018 11:12
Show Gist options
  • Save pfac/f35e96520f946bbcf93cd9407568c629 to your computer and use it in GitHub Desktop.
Save pfac/f35e96520f946bbcf93cd9407568c629 to your computer and use it in GitHub Desktop.
TIL 2018-11-20: String replacement in an Ansible Playbook

String replacement in an Ansible Playbook

Ansible is built with Python, and many things in there relate to the way Python works. String replacement, however, is slightly different. Trying to run the following playbook:

---
- tasks:
  - debug:
      msg: "{{ string.replace('something', 'some', 'any') }}"

will result in Ansible complaining about an undefined variable string being used.

Ansible Playbooks use the [Jinja2][jinja2] template engine, which provides a way to achieve this with a replace filter. Running the following playbook:

---
- tasks:
  - debug:
      msg: "{{ 'something' | replace('some', 'any') }}"

will successfully print out the debug message with anything.

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