The Better Things You Should Know About Ansible


1 – You can pass parameters to roles

It’s a good practice to create roles to organize your playbooks. Let’s say we want to create a role for installing Jenkins. The folder structure for this role could look something like this:

jenkins/
files/
templates/
tasks/
handlers/
defaults/

The folder defaults is used to store the values of default vars for a role. Inside of it we could have this main.yml file:

jenkins_port: 8080
jenkins_context_path: /jenkins
jenkins_home: /jenkins

You could overwrite the default variables, by passing different parameters to the role like so:

roles:
- { role: jenkins, jenkins_port: 8181, jenkins_home: '/jenkins1' }
- { role: jenkins, jenkins_port: 8080, jenkins_home: '/jenkins2' }