0

Ansible v2.10.8

I've tried the answers in Run an Ansible task only when the variable contains a specific string but they give me syntax errors.

for example, i want to check if my passed in variable contains "development" so I do this, as one of the answers on the post

- name: Set NodeJS version for development branch
  set_fact:
    nodejs_version: "{{ nodejs_versions[development] }}"
  when: '"development" in branch_name'

...and I get

TASK [nodejs : Set NodeJS version for development branch] **********************
fatal: [10.227.26.97]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'development' is undefined\n\nThe error appears to be in '/Users/xxxx/git-repos/ansible/exa-playbooks/roles/nodejs/tasks/main.yml': line 11, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- block:\n  - name: Set NodeJS version for development branch\n    ^ here\n"}

This one

- name: Set NodeJS version for development branch
  set_fact:
    nodejs_version: "{{ nodejs_versions[development] }}"
  when: branch_name | search("development")

...gives

TASK [nodejs : Set NodeJS version for development branch] **********************
fatal: [10.227.26.97]: FAILED! => {"msg": "The conditional check 'branch_name | search(\"development\")' failed. The error was: template error while templating string: no filter named 'search'. String: {% if exa_branch | search(\"development\") %} True {% else %} False {% endif %}\n\nThe error appears to be in '/Users/xxx/git-repos/ansible/exa-playbooks/roles/nodejs/tasks/main.yml': line 11, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- block:\n  - name: Set NodeJS version for development branch\n    ^ here\n"}

Any clues? TIA'

Chris F
  • 14,337
  • 30
  • 94
  • 192

1 Answers1

3

You just need to quote development in your task, because right now it's interpreting it as a variable name and attempting to parse it, rather than interpreting it as a string property name.

- name: Set NodeJS version for development branch
  set_fact:
    nodejs_version: "{{ nodejs_versions['development'] }}"
  when: '"development" in branch_name'
crock
  • 423
  • 3
  • 11