Ansible – Part 12 – Deploying Roles With Ansible Galaxy

So you have learned about Ansible Roles and the importance of using roles. Ansible Galaxy is a public library where you can find thousands of roles written by other ansible users and administrators. Ansible Galaxy is arranged with a searchable database where you can easily find suitable role which you can re-use in your ansible environment.

See all parts of  Automation with Ansible Guides here.

Login to Ansible Galaxy and explore the portal; you can also find the  docuemention on how to use roles from Ansible Galaxy, how to upload your roles to the library etc.

ansible-galaxy command line

You can use ansible-galaxy command to search, install or get information about the roles from Ansible Galaxy. In below example, we are searching for a role with word java.

[[email protected] ansible]# ansible-galaxy search "java"
Found 775 roles matching your search:
 Name                                                  Description
 ----                                                  -----------
 4ARMED.java8                                          Java 8 Installation for Debian-based OSes
 5KYDEV0P5.skydevops-java                              Installs and configures Oracle Java 1.8.x
 5KYDEV0P5.skydevops-maven                             Install and configure Apache Mvaen 3.x.x
 aaronpederson.python                                  Python is a programming language that lets you work quick
 aaronpederson.webgoat                                 Webgoat - a deliberately insecure JavaEE application to t
 abelboldu.zookeeper                                   Zookeeper cluster role for MidoNet deployments.
 aberwag.java                                          Java for Linux
 abessifi.fitnesse                                     Install FitNesse acceptance testing framework on GNU/Linu
 abessifi.java                                         Java for GNU/Linux systems
 abessifi.osb                                          Install and configure Oracle Service Bus
 abessifi.weblogic                                     Install and configure Oracle Weblogic Server
 abn.ec2-api-tools-java                                Install and configure AWS ec2-api-tools on a target node
 adamcin.ansible-zookeeper                             Ansible Zookeeper Role

Or another example with author name

[[email protected] galaxy-roles]# ansible-galaxy search elasticsearch --author geerlingguy
Found 5 roles matching your search:
 Name                              Description
 ----                              -----------
 geerlingguy.elasticsearch         Elasticsearch for Linux.
 geerlingguy.elasticsearch-curator Elasticsearch curator for Linux.
 geerlingguy.filebeat              Filebeat for Linux.
 geerlingguy.kibana                Kibana for Linux.
 geerlingguy.logstash              Logstash for Linux.

below command with option info will display the information about the role, like the author, license, creation date etc.

[[email protected] ansible]# ansible-galaxy info geerlingguy.git
Role: geerlingguy.git
        description: Git version control software
        active: True
        commit: 6d4696d59dfb6af69315978a47cb283b3aaaaa9c
        commit_message: Fix deprecation warnings in Ansible 2.5 for state 'present'.
        commit_url: https://github.com/geerlingguy/ansible-role-git/commit/6d4696d59dfb6af69315978a47cb283b3aaaa
        company: Midwestern Mac, LLC
        created: 2014-03-01T02:53:28.033086Z
        download_count: 86310
        forks_count: 61
        github_branch: master
        github_repo: ansible-role-git
        github_user: geerlingguy
        id: 431
        imported: 2018-04-04T19:50:26.043014-04:00
        is_valid: True
        issue_tracker_url: https://github.com/geerlingguy/ansible-role-git/issues
        license: license (BSD, MIT)
        min_ansible_version: 2.4
        modified: 2018-06-30T05:11:01.375198Z
        open_issues_count: 8
        path: [u'/root/.ansible/roles', u'/usr/share/ansible/roles', u'/etc/ansible/roles']
        role_type: ANS
        stargazers_count: 105
        travis_status_url: https://travis-ci.org/geerlingguy/ansible-role-git.svg?branch=master

And you can install the role with install option. I am creating a new directoty and installing new role there by mentioning the option -p.

[[email protected] ansible]# mkdir galaxy-roles
[[email protected] ansible]# cd galaxy-roles/
[[email protected] galaxy-roles]# ansible-galaxy install geerlingguy.git -p ./
- downloading role 'git', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-git/archive/2.0.2.tar.gz
- extracting geerlingguy.git to /root/ansible/galaxy-roles/geerlingguy.git
- geerlingguy.git (2.0.2) was installed successfully

And now we verify the content of the geerlingguy.git role installed.

[[email protected] galaxy-roles]# tree 
.
└── geerlingguy.git
    ├── defaults
    │   └── main.yml
    ├── LICENSE
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   ├── install-from-source.yml
    │   └── main.yml
    ├── tests
    │   ├── README.md
    │   ├── test-source.yml
    │   └── test.yml
    └── vars
        ├── Debian.yml
        ├── Fedora.yml
        ├── main.yml
        └── RedHat.yml

You can even install muliple roles with a file as below. The yml file contains the details of roles to be installed using ansible-galaxy.

$ ansible-galaxy install -r roles2install.yml

Once you have installed role, we can simply use it inside our playbook as normal.

  roles:
- geerlingguy.git

Managing installed roles

You can list and remove those installed roles with same ansible-galaxy command.

[[email protected] galaxy-roles]# ansible-galaxy list
- geerlingguy.git, 2.0.2

[[email protected] galaxy-roles]# ansible-galaxy remove geerlingguy.git
- successfully removed geerlingguy.git

Using ansible-galaxy to create role

You can use ansible-galaxy init commnd to initilize a role with default directory structure. Using –offline option can use if you want to do this activitiy without interacting with internet (Ansible Galaxy portal)

[[email protected] galaxy-roles]# ansible-galaxy init --offline mynewrole 
- mynewrole was created successfully
[[email protected] galaxy-roles]# tree
.
└── mynewrole
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

Then you can modify the file contents as usual.

Refer more on ansible-galaxy cli documentation.

See all parts of  Automation with Ansible Guides here.

Gini Gangadharan

Gineesh Madapparambath | Backpacker, Foodie, Techie | techbeatly.com|rovervibes.com

You may also like...

Leave a Reply

avatar
  Subscribe  
Notify of
%d bloggers like this: