Ryan-Neal Mes
Ryan-Neal Mes

Reputation: 6263

Why is conditional not working?

I am trying to write a simple conditional in Ansible and I am getting an error

- name: Add user key to AWS
  ec2_key:
    name: "{{ ansible_user_id }}_key"
    key_material: "{{ item }}"
    region: "{{ region }}"
  with_file: ~/.ssh/id_rsa.pub
  when: "development" == prefix

Error

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/Users/ryanme/sites/devops/aws-infrastructure/roles/ec2/tasks/main.yml': line 7, column 23, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  with_file: ~/.ssh/id_rsa.pub
  when: "development" == prefix
                      ^ here
This one looks easy to fix.  It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote.  For instance:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"

I have tried a couple of variations. This should be pretty straight forward, but I am obviously missing something. Any pointers would be appreciated.

Upvotes: 1

Views: 361

Answers (1)

Ryan-Neal Mes
Ryan-Neal Mes

Reputation: 6263

To resolve the problem I updated the script to

- name: Add user key to AWS
  ec2_key:
    name: "{{ ansible_user_id }}_key"
    key_material: "{{ item }}"
    region: "{{ region }}"
  with_file: "~/.ssh/id_rsa.pub"
  when: prefix == "development"

I needed to put with_file in double quotes as well since it starts with a special character.

Upvotes: 1

Related Questions