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
hostskeyword. For example:
---- name: My playbook hosts: myhosts
- Define the roles that you want to use in the
rolessection of the playbook. For example:
---- name: My playbook hosts: myhosts roles: - myrole
- Save the playbook file.
- Create a directory called
rolesin the same directory as the playbook file. - Inside the
rolesdirectory, create a directory for your role. For example, if your role is namedmyrole, create a directory calledmyrole. - Inside the
myroledirectory, 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
myroledirectory. - Save the playbook file and run it using the
ansible-playbookcommand, 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.
