# Playbook tasks vs Role tasks

**URL:** <https://forum.ansible.com/t/playbook-tasks-vs-role-tasks/22710>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [October 3, 2016, 8:41pm UTC](https://forum.ansible.com/t/playbook-tasks-vs-role-tasks/22710 "2016-10-03T20:41:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [October 3, 2016, 8:41pm UTC](https://forum.ansible.com/t/playbook-tasks-vs-role-tasks/22710/1 "2016-10-03T20:41:12Z")

</div>

I think I’m missing something fundamental.

I have a Playbook for doing Google Computer Engine (gce) stuff and it’s based on playbooks I found on the Internet. The tasks are defined within the playbook.

- name: google compute engine testing  
hosts: localhost  
connection: localhost  
gather\_facts: no

vars:  
project\_id: AAAA  
instance\_names: BBBB  
service\_account\_email: [me@AAAA.iam.gserviceaccount.com](mailto:me@AAAA.iam.gserviceaccount.com)  
credentials\_file: CCCCC  
image: ubuntu-1604-xenial-v20160721  
machine\_type: f1-micro  
zone: us-central1-b

tasks:

- name: create instance

gce:  
instance\_names: “{{ instance\_names }}”  
zone: “{{ zone }}”  
machine\_type: “{{ machine\_type }}”  
image: “{{ image }}”  
project\_id: “{{ project\_id }}”  
credentials\_file: “{{ credentials\_file }}”  
service\_account\_email: “{{ service\_account\_email }}”  
register: gce  
tags: google

- name: wait for ssh  
wait\_for:  
host: “{{ item.public\_ip }}”  
port: 22  
delay: 10  
timeout: 60  
state: started  
with\_items: “{{ gce.instance\_data }}”  
tags: google

How would I change this into a Role called gce-instance?

I assume the vars: and tasks would just move to the Role’s tasks/main.yml)?

But how do I get the below stuff into the Role?

- name: google compute engine testing  
hosts: localhost  
connection: localhost  
gather\_facts: no

Sample Playbook

- hosts: all  
vars:  
pre\_tasks:  
roles:
- { role: gce-instance }

I do not understand what

- name: google compute engine testing  
hosts: localhost  
connection: localhost  
gather\_facts: no

does. Is it setting up defaults for the below tasks?

tasks:

# gcloud compute machine-types list

# gcloud compute images list

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 3, 2016, 10:44pm UTC](https://forum.ansible.com/t/playbook-tasks-vs-role-tasks/22710/2 "2016-10-03T22:44:02Z")

</div>

first of all:

connection: localhost

is wrong, you can omit it as the ‘implicit localhost’ already uses a local connection or use:

connection: local

So roles are meant to reuse tasks, even so you still need to setup target hosts, in the case of cloud modules that ‘target’ is normally localhost (target is where the module executes).

That is why you always need a play, as roles do not target hosts, just provide a set of tasks and resources to run.
