为什么条件不起作用?

时间:2016-02-02 09:46:29

标签: ansible

我正在尝试在Ansible中编写一个简单的条件,我收到错误

- 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! 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"

我尝试了几种变体。这应该是非常直接的,但我显然缺少一些东西。任何指针都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

要解决此问题,我将脚本更新为

- 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"

我需要将with_file放在双引号中,因为它以特殊字符开头。