Los roles de Ansible nos permitirán reutilizar partes de código entre playbooks simplemente incluyendo el role pertinente, de este modo cuando necesitemos realizar algún cambio en el role solo tendremos que tocar un único fichero.
En este ejemplo tendremos un playbook base que incluirá tres roles, uno de configuración de screen, otro de instalación de utilidades básicas en Gentoo y otro de utilidades básicas en Ubuntu.
Creamos la estructura de directorios necesaria:
mkdir -p roles/configureScreen/files
mkdir -p roles/configureScreen/tasks
mkdir -p roles/installUtilitiesGentoo/tasks
mkdir -p roles/installUtilitiesUbuntu/tasks
mkdir -p roles/configureScreen/tasks
mkdir -p roles/installUtilitiesGentoo/tasks
mkdir -p roles/installUtilitiesUbuntu/tasks
Creamos los ficheros necesarios:
vi roles/configureScreen/files/.screenrc
caption always "%{= kw}%-w%{= gW}%n %t%{-}%+w %-= - %Y-%m-%d %C:%s"
vi roles/configureScreen/tasks/main.yml
- name: Copy .screenrc file
copy:
src: "{{ role_path }}/files/.screenrc"
dest: /root/.screenrc
owner: root
group: root
mode: '0644'
vi roles/installUtilitiesGentoo/tasks/main.yml
- name: Install basic utilities Gentoo
portage:
package: "{{item}}"
state: present
with_items:
- sys-fs/ncdu
- sys-process/htop
- net-analyzer/tcpdump
- net-analyzer/ngrep
- app-misc/screen
vi roles/installUtilitiesUbuntu/tasks/main.yml
- name: Install basic utilities Ubuntu
apt:
name: ['ncdu', 'htop', 'tcpdump', 'ngrep', 'screen', 'strace', 'net-tools', 'python-apt']
update_cache: yes
La estructura gráficamente quedarÃa asÃ:
roles/
├── configureScreen
│ ├── files
│ └── tasks
│ └── main.yml
├── installUtilitiesGentoo
│ └── tasks
│ └── main.yml
└── installUtilitiesUbuntu
└── tasks
└── main.yml
Finalmente escribimos el playbook que hará uso de los roles:
vi base.yml
- hosts: all
gather_facts: yes
tasks:
- name: groupByOS
group_by: key=os_{{ ansible_distribution }}
- hosts: os_Gentoo
roles:
- installUtilitiesGentoo
- configureScreen
- hosts: os_Ubuntu
roles:
- installUtilitiesUbuntu
- configureScreen
Ejecutamos el playbook:
ansible-playbook base.yml
PLAY [all] ********************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************************************
ok: [kr0mtest2]
ok: [kr0mtest3]
ok: [test00]
ok: [kr0mtest4]
ok: [kr0mtest]
TASK [group_by] ***************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [kr0mtest]
ok: [kr0mtest2]
ok: [kr0mtest3]
ok: [kr0mtest4]
ok: [test00]
PLAY [os_Gentoo] **************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************************************
ok: [kr0mtest2]
ok: [kr0mtest3]
ok: [kr0mtest4]
ok: [kr0mtest]
TASK [installUtilitiesGentoo : Install basic utilities Gentoo] ****************************************************************************************************************************************************************************************************************************************************************
ok: [kr0mtest3] => (item=sys-fs/ncdu)
ok: [kr0mtest4] => (item=sys-fs/ncdu)
ok: [kr0mtest2] => (item=sys-fs/ncdu)
ok: [kr0mtest] => (item=sys-fs/ncdu)
ok: [kr0mtest3] => (item=sys-process/htop)
ok: [kr0mtest4] => (item=sys-process/htop)
ok: [kr0mtest2] => (item=sys-process/htop)
ok: [kr0mtest] => (item=sys-process/htop)
ok: [kr0mtest3] => (item=net-analyzer/tcpdump)
ok: [kr0mtest4] => (item=net-analyzer/tcpdump)
ok: [kr0mtest2] => (item=net-analyzer/tcpdump)
ok: [kr0mtest] => (item=net-analyzer/tcpdump)
ok: [kr0mtest3] => (item=net-analyzer/ngrep)
ok: [kr0mtest4] => (item=net-analyzer/ngrep)
ok: [kr0mtest2] => (item=net-analyzer/ngrep)
ok: [kr0mtest] => (item=net-analyzer/ngrep)
ok: [kr0mtest3] => (item=app-misc/screen)
ok: [kr0mtest4] => (item=app-misc/screen)
ok: [kr0mtest2] => (item=app-misc/screen)
ok: [kr0mtest] => (item=app-misc/screen)
TASK [configureScreen : Copy .screenrc file] **********************************************************************************************************************************************************************************************************************************************************************************
ok: [kr0mtest3]
ok: [kr0mtest2]
ok: [kr0mtest4]
ok: [kr0mtest]
PLAY [os_Ubuntu] **************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************************************************************
ok: [test00]
TASK [installUtilitiesUbuntu : Install basic utilities Ubuntu] ****************************************************************************************************************************************************************************************************************************************************************
[WARNING]: Could not find aptitude. Using apt-get instead
changed: [test00]
TASK [configureScreen : Copy .screenrc file] **********************************************************************************************************************************************************************************************************************************************************************************
changed: [test00]
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************************************************************
kr0mtest : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
kr0mtest2 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
kr0mtest3 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
kr0mtest4 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test00 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0