To use an Ansible role from a playbook, you can follow these steps:
- Create a playbook file, for example,
playbook.yml
. - In the playbook file, define the hosts or host groups that the playbook will apply to, using the
hosts
keyword. For example:
---
- name: My playbook
hosts: myhosts
- Define the roles that you want to use in the
roles
section of the playbook. For example:
---
- name: My playbook
hosts: myhosts
roles: - myrole
- Save the playbook file.
- Create a directory called
roles
in the same directory as the playbook file. - Inside the
roles
directory, create a directory for your role. For example, if your role is namedmyrole
, create a directory calledmyrole
. - Inside the
myrole
directory, create the necessary files and directories for your role, includingtasks/main.yml
,vars/main.yml
,handlers/main.yml
, and any other files that are required for your role. - Save the files and directories for your role in the appropriate locations within the
myrole
directory. - Save the playbook file and run it using the
ansible-playbook
command, specifying the location of the playbook file. For example:
ansible-playbook playbook.yml
This will run the playbook and apply the role to the specified hosts or host groups. Ansible will automatically look for the role in the roles
directory and apply it to the hosts as specified in the playbook.