Best way to Include Roles in tasks

53 views
Skip to first unread message

Colin Byrne

unread,
Dec 28, 2015, 7:08:37 PM12/28/15
to Ansible Project
So I used to have one fairly long main.yml playbook that was used to perform a number of similar-tasks, one of which was to install rbenv, the ruby management tool.

---

  - name: create group titled deploy

    group: name=deploy state=present

  - name: update apt-cache

    sudo: yes

    apt: update_cache=yes

  - name: create deploy user

    user: name=deploy group=deploy groups=sudo comment="Deployment User" home="/home/deploy" shell="/bin/bash" 

  - name: download rbenv

  ...  

  - name: install rbenv

  ... 

  - name: configure rbenv

  ...  

  - name: Install the kitchen sink

    action: apt name={{item}} state=present update_cache=true

    sudo: yes

    with_items:

      - libqtwebkit-dev

      - node

  -  lots of other tasks 
  ...

I found a nice galaxy role that better suits my needs for installing rbenv, and I now want to include it to run in the same order as previously, but I am confused as to how the documentation recommends to do this and the syntax (seemingly with dependencies or includes?). It is hard to decipher wether I will have to split it into three roles (the pre-rbenv, rbenv, post-rbenv) which would really seem inconvenient as the three would always be used together. I suspect this is a pretty common dilemma...

Thank you all for the help so far, this forum has really made is a pleasure learning ansible. 

Borys Borysenko

unread,
Dec 29, 2015, 4:03:50 AM12/29/15
to Ansible Project
Hello Colin,

The most simple way I guess is just to split your playbook with pre_tasks, roles and tasks sections. Something similar to:

---
- hosts: all
  vars: []
  pre_tasks:
  - name: create group titled deploy
    group: name=deploy state=present
  - name: update apt-cache
    sudo: yes
    apt: update_cache=yes
  - name: create deploy user
    user: name=deploy group=deploy groups=sudo comment="Deployment User" home="/home/deploy" shell="/bin/bash" 
  roles:
    - external-rbenv-role
  tasks:
  - name: Install the kitchen sink
    action: apt name={{item}} state=present update_cache=true
    sudo: yes
    with_items:
     - libqtwebkit-dev
     - node
  -  lots of other tasks 
  post_tasks: []

Regards,
Borys

вторник, 29 декабря 2015 г., 2:08:37 UTC+2 пользователь Colin Byrne написал:

Colin Byrne

unread,
Dec 29, 2015, 4:12:03 AM12/29/15
to Ansible Project
Hey, Thanks for the reply... This is already in a role though, so would calling a role from a role make sense or even work? 
Thanks, C

chris meyers

unread,
Dec 31, 2015, 11:52:50 AM12/31/15
to Ansible Project
The only way you can invoke a role from within a role is via meta/main.yml You may not explicitly invoke a role within a role like you do tasks.
Reply all
Reply to author
Forward
0 new messages