How to pass dynamic roles to Ansible playbook

时间:2019-04-17 02:05:54

标签: ansible ansible-role

Standard way of defining an Ansible playbook with roles is:

--- 
- hosts: webservers 
  roles:
    - common 
    - webservers

I would like to pass a single command line variable for the Ansible role. I mean to say there should be a single Ansible role which would be a variable and I should be able to pass the choice of role (common or webserver) from outside.

Please let me know if and how we can achieve this:

roles:
  - {{ choice }}

1 个答案:

答案 0 :(得分:1)

play.yml

中使用include_role
- hosts: webservers
  tasks:
    - include_role:
        name: "{{ choice }}"

并运行它

# ansible-playbook -e "choice=common" play.yml