申请条件成为/ become_user

时间:2017-05-19 23:41:54

标签: ansible ansible-2.x

- name: Create database if database does not exist
  shell: "createdb -h {{ db_host }} -O analytics {{ database }}"
  become: yes
  become_user: analytics
  when:
    database_exists.stdout != "1"

目前我有一个像上面这样的游戏但我想要的是成为/ become_user属性也是有条件的。我想要它,当ansible_env.USER =' analytics'设置变为否,当它不是“分析”时把它设置为是。有人知道这是否可以在ansible中使用?

1 个答案:

答案 0 :(得分:4)

您可以在Jinja2表达式({{ ... }})中使用不同的条件:

- name: Create database if database does not exist
  shell: "createdb -h {{ db_host }} -O analytics {{ database }}"
  become: "{{ 'yes' if ansible_env.USER == 'analytics' else 'no' }}"
  become_user: analytics
  when:
    database_exists.stdout != "1"