cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Starfighter Starfighter
Starfighter
  • 1,633 Views

Plays

Hello all,

Got a question regarding a play.  Specifically, what actually defines the beginning of a play in an Ansible playbook?

The great majority of sample playbooks that I've looked at, a play begins with the 
following:      - name:  This play will do whatever I say

 

While doing some reading on the mothership site (https://docs.ansible.com), I saw a sample playbook that began with the following:   - hosts:  webservers

 

Okay, what I'm featuring here are the items (keywords) highlighted in red.  Apparently, "name" and "hosts" can begin the definition of a play in a playbook.  Are there other keywords that can be used to define the start of a play?  

I really want to get the fundamentals down so that I can eventually author my own playbooks.  As you know, that will require my understanding of the mechanics of the YAML syntax, playbook formatting, etc.

As always, thanks in advance for your assistance.

 

 

 

Trevor "Red Hat Evangelist" Chandler
Labels (1)
0 Kudos
2 Replies
matteoclc
Flight Engineer
Flight Engineer
  • 1,623 Views

Hello Trevor,

you can start your playbook with any valid keyword in its context because keywords at the same level can be swapped in any order.

At the playbook level you can have these keywords in example

name
tasks
vars
gather_facts
roles
hosts
become

 You can put them in any order, and so a valid play definition could be

- hosts: webservers
  become: no
  vars:
    service: httpd
  tasks:
  - name: install package
    yum:
      name: "{{ service }}"
      state: present

 but also, if you prefer

- become: no
  vars:
    service: httpd
  tasks:
  - name: install package
    yum:
      name: "{{ service }}"
      state: present
  hosts: webservers

 

So it's just a matter of what is better to do in order to write a good and readable playbook. Maybe the first information i expect to read is a short definition of what this playbook do, and so i'll put "name" at the first place (but often i'll skip this step). Then i like to see which hosts my playbook targets. Then i'll put other "parameters" like become, gather facts, and finally tasks or roles. It's very up to you but there are some common practice that you'll want to follow.

At the very end i think the best way to learn Ansible is to use it: start write some playbooks for basics tasks and then improve them, step by step. Don't look for perfection, it's a waste of time and an huge obstacle.

good luck!

--
Matteo Calcagnini
Trevor
Starfighter Starfighter
Starfighter
  • 1,618 Views

Hello Matteo,

I know I can always count on you to come to my rescue!

As usual, you've laid out an explanation that makes perfect sense, and is serving to build my Ansible muscle.

Your advice on just getting in there, and writing some playbooks makes sense for sure.  However, because of the way my mind works, it helps me tremendously to know what a particular part does, and where it goes.  I hope this doesn't frustrate you in not wanting to respond to any of my future queries.

Again, many, many thanks for another great lesson!

 

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.