---
# System prepended metadata

title: Ansible Roles

---

# Ansible Roles

[toc]

#### Ansible Galaxy Commands
````yaml=
ansible-galaxy role <arguments>

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the actual GitHub repository.
    list       Show the name and version of each role installed in the roles_path.
    search     Search the Galaxy database by tags, platforms, author and multiple keywords.
    import     Import a role into a galaxy server
    setup      Manage the integration between Galaxy and the given source.
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy
````

#### Working with Ansible Roles
````yaml=
Check local roles:
ansible-galaxy role list

Create a local role:
ansible-galaxy init <role-name>

Explore Ansible Galaxy:
galaxy.ansible.com

Install a role from Ansible Galaxy:
ansible-galaxy install <authorname.rolename>

Validate the role installation:
ansible-galaxy role list
````
#### Write a playbook to call the role
````yaml=
---
- name: nginx using role
  hosts: nodes
  become: yes
  tasks:
    - name: update cache
      apt: update_cache=yes

    - name: Install and work with nginx
      include_role:
        name: geerlingguy.nginx
````
#### Run the Playbook
````yaml=
ansible-playbook role-play.yaml
````
#### Validate the changes
````yaml=
curl localhost (or elinks)
````
#### Remove a role from Ansible Controller
````yaml=
Syntax:
ansible-galaxy role remove <role_name>

Example: 
ansible-galaxy role remove geerlingguy.nginx

Validate the role has been removed:
ansible-galaxy role list
````


#### Configure a LAMP server using Ansible Roles

````yaml=
Install Roles:

ansible-galaxy install geerlingguy.mysql
ansible-galaxy install geerlingguy.apache
ansible-galaxy install geerlingguy.php

Create an Ansible playbook named lamp.yml with the following contents:

---
- hosts: all
  roles:
    - geerlingguy.mysql
    - geerlingguy.apache
    - geerlingguy.php


Run the playbook:
ansible-playbook -i path/to/custom-inventory lamp.yml

Validation:

````



