Skip to content

Latest commit

 

History

History
116 lines (82 loc) · 4.36 KB

6.Roles and Galaxy.MD

File metadata and controls

116 lines (82 loc) · 4.36 KB

Roles and Galaxy

While it is possible to write a playbook in one very large file (and you might start out learning playbooks this way), eventually you’ll want to reuse files and start to organize things.

Roles are ways of automatically loading certain vars, files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.

The anatomy of a role.

- hosts: webservers
  vars_files:
    - vars/main.yml
  roles:
    - { role: geerlingguy.apache }
$ ansible-galaxy

Notable Options for ansible-galaxy

Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help]

  • init
  • install
  • delete
  • list

Exercises

  • What are roles?
  • Initialize your first ansible role.
  • Install your first ansible role.
  • Why can't you install your first ansible-role?
  • Look ahead at configuration(http://docs.ansible.com/ansible/intro_configuration.html) and set the role folder to a folder you have permissions to.
  • What happens when you run the playbook multiple times?
  • What is that concept called?

Let's continue on our path forward. We talked about the structure of the ansible assumes and we played a fast one on you. That structure is the assumed structure for an ansible role, not a playbook. Ansible roles are well encapsulated plays. That means, if we are doing our job and abstracting our infrastructure code, we will be using re-usable roles more than we are using plays in playbooks.

  • defaults
  • main.yml
  • files
  • all files in this directory can automatically be found as source files.
  • handlers
  • main.yml
  • meta
  • main.yml
  • tasks
  • main.yml
  • templates
  • all files in this directory can automatically be found as source for templates.
  • vars
  • main.yml

Defaults are high level overriderable variables. Variables are lower level overridable variables. Why defaults and why vars? If we include a role, a host or group var cannot override a default.

Handlers are tasks that are executed at the end of a playbook, normally restarting services or a database migrations, etc.

Meta is data that is used as a developer to inform you about the playbook and ansible-galaxy uses it to help share and install roles on other people's machines.

Tasks are ansible tasks like we have seen before.

Files and templates are source directories for common tasks/modules that use file and templates.

Getting new Roles

ansible-galaxy install -r requirements.yml

You can use ansible galaxy to install ansible roles from the internet. Ansible-galaxy isn't just a command line tool but a website! You can peruse and install roles and you can also manage your roles with a requirements.yml file.

Install our requirements.yml. Inspect the roles directory. What do we see?

Common modules

Every role is different but there are two modules that are very common. In fact they are so common, they have there own folders by default. File and Templates are auto discovered directories for the file and template modules.

Your very own Role!

ansible-galaxy init

Now that you have initialized your very own role. Open it up and note which folders don't mean anything to you yet. Let's design our first reusable and useful role in ansible.

Execercise

You are a devops engineer charged with maintaining your company's servers. We are going to design our playbook and role to reflect our desired server state.

  1. The first server is a jenkins instance. We will reuse our existing galaxy role.
  2. The second server is a gitlab instance. We will find a new galaxy role.
  3. The third server will be an apache instance serving static content. We will use a custom role to create this.
  • The server needs to have httpd installed
  • The server needs to have httpd running
  • The server should show a custom home page instead of the default page. The page might change frequently and a variable should be used to display hello {{ custom_name }}. This will require use of module that can search the template directory. The files placed in the /var/www/html directory will be served by apache.
  • The httpd service should be restarted when the custom_name variable is changed. This will require a handler and a notify statement.
  • Once we have the service running, change the variable in a few locations to ensure the restart is happening successfully.

see

  • inventory_setupv3.yml
  • play1v5.yml