Anan
Anan

Reputation: 3

ansible roles as command line arg

Is there any way to pass a role to ansible-playbook cli as a command line arg?

This works:

 roles:
    - common
    - web

when I run:

ansible-playbook -i hosts site.yml

I tried :

roles:
      - $role1
      - $role2

then ran:

ansible-playbook -i hosts site.yml --extra-vars="roles=common"

or

ansible-playbook -i hosts site.yml --extra-vars "roles=common"

but no success

thanks

Upvotes: 0

Views: 3532

Answers (1)

Bruce P
Bruce P

Reputation: 20719

You can use tags to do this. You can tag each role individually, or tag groups of roles with the same tag, then simply specify one or more tags on the command line to indicate which ones you want run.

Here's how you tag a role:

roles:
  - { role: webserver, tags: [ 'web', 'apache' ] }
  - { role: webapp, tags: [ 'web', 'app' ] }

In this example, if you specify --tags=web when running this playbook then all the tasks in both of these roles will be run. If only the tag apache or app is specified then only the tasks in the appropriate role is run. And of course if no tags are specified then everything is run as well.

Upvotes: 1

Related Questions