Playbook tasks vs Role tasks

37 views
Skip to first unread message

b...@tanners.org

unread,
Oct 3, 2016, 6:11:54 PM10/3/16
to Ansible Project
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: m...@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
    #
    - 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

Brian Coca

unread,
Oct 3, 2016, 6:44:08 PM10/3/16
to ansible...@googlegroups.com
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.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages