Ansible Refactoring Part 4 - Roles Refactor First Pass
Part 4 - Roles Refactor
This is Part 4 of an Ansible Refactoring Series - Start Here
In our second pass through the project we will take the now more modular solution a step further and break it into roles. In this case we will basically map each of the 3 playbooks from Part 3 into 1 role each
-
Create a new branch refactor-pass-02-roles
-
Create a roles sub-directory in the repo
-
Create each role
-
Copy the tasks from your playbook into the roles
tasks/main.yml -
Consider moving some/all vars into the roles
defaults/main.ymlorvars/main.yml
-
Turning the Postgres Play into a Role
We will start here and document the steps for our first role. The remainimg 2 roles for flask and HAProxy will basically repeat the same pattern.
-
Create a new branch
refactor-pass-02-rolesgit checkout -b refactor-pass-02-rolesSample OutputSwitched to a new branch 'refactor-pass-02-roles' -
Make a roles sub-directory
mkdir roles -
Use
ansible-galaxyto create the skeleton for thepostgresroleansible-galaxy init roles/postgresSample Output- Role roles/postgres was created successfullyTipThe
treecommand, not always installed, is useful to visualize directory structures. Try this,sudo yum install tree -ythentree rolesto see your role:Sample Outputroles └── postgres ├── defaults │ └── main.yml ├── files ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── README.md ├── tasks │ └── main.yml ├── templates ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml -
Start by creating your roles
tasks/main.ymlThe easiest starting place is to copy your provision_database_tier.yml into the roles
tasks/main.yml.cp provision_database_tier.yml roles/postgres/tasks/main.yml -
Now edit and fix the
tasks/main.ymlDelete everything except the tasks themselves, no need for to keep the
tasks:directive and fix the indentation by moving everything left (vimis extremely good for this with commands like<<and n`<<` where n is the count operator. eg5<<outdents 5 lines)NoteDon’t forget to delete the
handlersection at the bottom of the play.Your top 10 lines should now look like this:
head roles/postgres/tasks/main.ymlSample Output--- - name: Install Postgres packages package: name: "{{ __package }}" state: present loop: - "{{ postgres_rhel7_repo }}" - "{{ postgres_packages }}" - "{{ postgres_library }}" loop_control: -
Now repeat a similar process to steps 4 and 5 above except this time with the roles
handlers/main.ymlOnce you have finished your
roles/postgres/handlers.ymlshould look like this:--- - name: restart_postgres service: name: "{{ postgres_service }}" state: restarted -
Roles have in-built support for templates and your postgres playbook referenced
templates/pg_hba.conf.j2-
Move your
templates/pg_hba.conf.j2toroles/postgres/templates/ -
Update your
roles/postgres/tasks/main.ymlto remove the path to thepg_hba.conf.j2template as the role knows "where to look"Your roles
templatetask inroles/postgres/tasks/main.ymlshould now look like this- name: Setup Postgres for remote password auth template: src: pg_hba.conf.j2 dest: "{{ postgres_10_data_dir }}/pg_hba.conf" notify: restart_postgres
-
-
Update your
provision_database_tier.ymlplaybook to use the rolepostgresand delete the oldtasksandhandlerscodeYour
provision_database_tier.ymlshould look like this:--- - name: Deploy, configure, and populate Postgres 10 hosts: database_servers become: true gather_facts: false tags: - database_servers roles: - postgres -
Finally test your new role by running the
provision_database_tier.ymleither directly or viasite.ymlYou mean want to delete your application first via
ansible-playbook teardown-app.ymlSuccess (hopefully)
If not debug your issues until you can successfully deploy and configure Postgres by your role.
-
Commit your changes
git add roles git commit -am "Moved postgres playbook to use new postgres role" -
Push your changes to your repo
If you have set up your own fork
git pushNotegitmay ask you to perform some adminstrative commands and if it is your first push on this branchgit push --set-upstream origin refactor-pass-02-roles
Repeat the above Process for your remaining playbooks
Once you have converted both provision_app_tier.yml and provision_load_balancer_tier.yml to roles based playbooks re-run the whole deploy end to end
ansible-playbook teardown-app.yml
ansible-playbook site.yml
Solution
One possible solution can be seen here in the solution refactor-pass-02-roles branch, either:
-
Browse to the
refactor-pass-02-rolesbranch -
Download the solution and checkout the
refactor-pass-02-rolesbranchgit clone https://github.com/tonykay/solution_ansible_flask_app_loader_all_in_one cd solution_ansible_flask_app_loader_all_in_on git checkout refactor-pass-02-roles
Next Steps
Unfortunately your roles, whilst working, are not complete.
At this point they cannot be used standalone as they lack the necessary variables which are all being acquired through your group_vars.
In the next lab we will look at what variables belong inside the roles, and where, and what variables should remain external.
Then we will also clean up your roles, removing redundant files etc.
In Part 5 we will continue to work with roles and enhance them to be re-usable across multiple projects with sensible default behavior "out of the box".