1 what're roles?
A role is a collection of tasks, handlers, vars that are organized in a known file structure. The main purpose of roles is to share playbooks with others.
A role represents a certain function/state of targetted servers, like web_server, db_server, or SELinux_enabled.
2 define roles
The quickest way to create a role is to use "ansible-galaxy" to initialize the role structure.
$ ansible-galaxy role init myrole
- Role myrole was created successfully
$ tree myrole/
myrole/
|-- README.md
|-- defaults
| `-- main.yml
|-- files
|-- handlers
| `-- main.yml
|-- meta
| `-- main.yml
|-- tasks
| `-- main.yml
|-- templates
|-- tests
| |-- inventory
| `-- test.yml
`-- vars
`-- main.yml
8 directories, 8 files
Then we can write tasks/handlers for the role just as in normal playbooks.
$ cat myrole/tasks/main.yml
---
# tasks file for myrole
- name: task1
debug:
msg: myrole---task1
3 use roles
3.1 where does ansible-playbook find roles?
ansible-playbook use config item "DEFAULT_ROLES_PATH" to locate roles.
$ ansible-config list | grep -A 10 ROLES_PATH
DEFAULT_ROLES_PATH:
default: ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
description: Colon separated paths in which Ansible will search for Roles.
env:
- name: ANSIBLE_ROLES_PATH
expand_relative_paths: true
ini:
- key: roles_path
section: defaults
name: Roles path
type: pathspec
yaml:
key: defaults.roles_path
An example to set it in ansible.cfg:
$ cat ansible.cfg
[defaults]
inventory=./hosts
host_key_checking = False
roles_path = ./roles
Now, in order to let ansible to find the role "myrole" just created, the "myrole" folder is to be put under ./roles.
$ mv myrole ./roles/
3.2 different ways to use roles
(1)The original/old way to statically include roles in a play
Roles used in such a way run before any other tasks within the same play, no matter what order roles are included.
$ cat testbook.yml
---
- hosts: localhost
tasks:
- name: task1
debug:
msg: task1
roles:
- myrole
$ ansible-playbook testbook.yml
PLAY [localhost] ***********************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [localhost]
TASK [myrole : task1] ******************************************************************************
ok: [localhost] => {
"msg": "myrole---task1"
}
TASK [task1] ***************************************************************************************
ok: [localhost] => {
"msg": "task1"
}
PLAY RECAP *****************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(2) include/import roles just like normal tasks
Using "import_role" or "include_role" to use roles in a play treats roles just like normal tasks defined in the play. The role tasks run in order based on where the role was included.
$ cat testbook.yml
---
- hosts: localhost
tasks:
- name: task1
debug:
msg: task1
- import_role:
name: myrole
$ ansible-playbook testbook.yml
PLAY [localhost] ***********************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [localhost]
TASK [task1] ***************************************************************************************
ok: [localhost] => {
"msg": "task1"
}
TASK [myrole : task1] ******************************************************************************
ok: [localhost] => {
"msg": "myrole---task1"
}
PLAY RECAP *****************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4 share roles with ansible-galaxy
4.1 search roles on the web
https://galaxy.ansible.com/
4.2 download roles
$ ansible-galaxy role install --roles-path ./roles geerlingguy.apache
No comments:
Post a Comment