Ansible/Roles: Difference between revisions
From charlesreid1
(Created page with "=Playbook Roles= ==What are Ansible roles?== Roles allow you to split your playbook into different parts for different servers. For example, a webapp with a database backen...") |
|||
| Line 7: | Line 7: | ||
For example, a webapp with a database backend can define a webserver role and a database role, and it becomes much easier to modify the playbook to run these on the same host or on different hosts. | For example, a webapp with a database backend can define a webserver role and a database role, and it becomes much easier to modify the playbook to run these on the same host or on different hosts. | ||
==Feature: pre-tasks and post-tasks== | |||
In the playbook you can specify a pre-task and a post-task for a role. | |||
For example, suppose you want to update aptitude before deploying the web server, and you want to send a notification to Slack when you are finished. | |||
Then you could use the following playbook, which defines a <code>pre_tasks</code> list of things to do before the roles are defined, and a <code>post_task</code> list of things to do once the roles have been carried out. | |||
<pre> | |||
- name: deploy mezzanine on vagrant | |||
hosts: web | |||
vars_files: | |||
- secrets.yml | |||
pre_tasks: | |||
- name: update the apt cache | |||
apt: update_cache=yes | |||
roles: | |||
- role: mezzanine | |||
database_host: "{{ hostvars.db.ansible_eth1.ipv4.address }}" | |||
live_hostname: 192.168.33.10.xip.io | |||
domains: | |||
- 192.168.33.10.xip.io | |||
- www.192.168.33.10.xip.io | |||
post_tasks: | |||
- name: notify Slack that the servers have been updated | |||
local_action: > | |||
slack | |||
domain=acme.slack.com | |||
token={{ slack_token }} | |||
msg="web server {{ inventory_hostname }} configured" | |||
</pre> | |||
=Flags= | =Flags= | ||
{{AnsibleFlag}} | {{AnsibleFlag}} | ||
Revision as of 17:16, 13 November 2018
Playbook Roles
What are Ansible roles?
Roles allow you to split your playbook into different parts for different servers.
For example, a webapp with a database backend can define a webserver role and a database role, and it becomes much easier to modify the playbook to run these on the same host or on different hosts.
Feature: pre-tasks and post-tasks
In the playbook you can specify a pre-task and a post-task for a role.
For example, suppose you want to update aptitude before deploying the web server, and you want to send a notification to Slack when you are finished.
Then you could use the following playbook, which defines a pre_tasks list of things to do before the roles are defined, and a post_task list of things to do once the roles have been carried out.
- name: deploy mezzanine on vagrant
hosts: web
vars_files:
- secrets.yml
pre_tasks:
- name: update the apt cache
apt: update_cache=yes
roles:
- role: mezzanine
database_host: "{{ hostvars.db.ansible_eth1.ipv4.address }}"
live_hostname: 192.168.33.10.xip.io
domains:
- 192.168.33.10.xip.io
- www.192.168.33.10.xip.io
post_tasks:
- name: notify Slack that the servers have been updated
local_action: >
slack
domain=acme.slack.com
token={{ slack_token }}
msg="web server {{ inventory_hostname }} configured"
Flags