Ansible syntax error when using variable in become

2019-07-23 14:11发布

问题:

I'm using Ansible 2.0.1.0.

In my main.yml I have this task:

- name: hoge
  hosts: hoge2
  connection: docker
  become: yes
  become_user: {{ansible_user}}

When executing, I get this error message:

ERROR! Syntax Error while loading YAML.

The error appears to have been in '/Users/fuga/Docker/ansible/main.yml': line 16, column 17, but maybe elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  become: yes
  become_user: {{ansible_user}}
                ^ here

How can I fix the error?

回答1:

In YAML a { starts a dictionary. Therefore you need to quote the expression.

become_user: "{{ansible_user}}"

See Gotchas in the Ansible YAML docs.



标签: ansible