This page looks best with JavaScript enabled

Roles in Ansible

 ·  🎃 kr0m

Ansible roles allow us to reuse code parts between playbooks simply by including the relevant role, so when we need to make any changes to the role we only have to touch a single file.

In this example, we will have a base playbook that will include three roles, one for screen configuration, another for installing basic utilities in Gentoo, and another for basic utilities in Ubuntu.

We create the necessary directory structure:

mkdir -p roles/configureScreen/files
mkdir -p roles/configureScreen/tasks
mkdir -p roles/installUtilitiesGentoo/tasks
mkdir -p roles/installUtilitiesUbuntu/tasks

We create the necessary files:

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

The graphical structure would be as follows:

roles/
├── configureScreen
│   ├── files
│   └── tasks
│       └── main.yml
├── installUtilitiesGentoo
│   └── tasks
│       └── main.yml
└── installUtilitiesUbuntu
    └── tasks
       └── main.yml

Finally, we write the playbook that will use the 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

We run the 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   
If you liked the article, you can treat me to a RedBull here