Getting started with "properly structured playbooks!?"

Hi Forum,

If I write playbooks using the “ansible-galaxy init” command:

ansible-galaxy init standardisationBot

Which gives me this

shr1mac1:standardisationBot nsheridan$ tree

.

├── README.md

├── defaults

│ └── main.yml

├── handlers

│ └── main.yml

├── meta

│ └── main.yml

├── tasks

│ └── main.yml

├── tests

│ ├── inventory

│ └── test.yml

└── vars

└── main.yml

6 directories, 8 files

shr1mac1:standardisationBot nsheridan$

How do I associate a variable with host R1? more’s the point where do I put anything??? Can anyone help?

Thanks nik

Hey Nicholas,

there are many possible locations where you could define it. Are you aware that there is a difference between “roles” and “playbooks”? The filelayout you created is a role- layout. A role contains a set of tasks which are applied to a host. The decision which host the role is running on is already made. So every variable you define in inside the role is available to that host.

So, you could define the variable inside the defaults/main.yml (least precedence), in the vars/main.yml or directly in the task.

Here is a complete list:

  • role defaults [1]
  • inventory INI or script group vars [2]
  • inventory group_vars/all
  • playbook group_vars/all
  • inventory group_vars/*
  • playbook group_vars/*
  • inventory INI or script host vars [2]
  • inventory host_vars/*
  • playbook host_vars/*
  • host facts
  • play vars
  • play vars_prompt
  • play vars_files
  • role vars (defined in role/vars/main.yml)
  • block vars (only for tasks in block)
  • task vars (only for the task)
  • role (and include_role) params
  • include params
  • include_vars
  • set_facts / registered vars
  • extra vars (always win precedence)

Check out this doc: http://docs.ansible.com/ansible/latest/playbooks_variables.html

Hi Daniel,

Yes, I think I understand the difference between a role and a playbook - but it’s all still a bit hazy - I have been probably doing super bad practice (incorporating variables into an inventory file) recently, so I really trying to do it properly!

Thanks a million for the list - really helpful!

Cheers nik