11

As I understand, each playbook takes one hosts entry. I want to know that if I create a container playbook that includes other playbooks, can I parameterize the hosts for each playbook include. So something like

---
- include playbook_1.yml
    hosts: tag_postgres
- include: playbook_2.yml
    hosts: tag_rabbitmq

I am able to put all different playbooks in one script and call, but then this way I am not able to reuse some set_fact from one playbook into another and hence there is a lot of task duplication.

Another corollary of the question is, can I launch ec2s on amazon which would have

hosts: localhost

and configuration of the launched ec2s, which would basically configure inventory from -i ec2.py, and have hosts specified as

hosts: tag_<some_tag>

happening through the same playbook or a set of included playbooks (different roles)?

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
Pankaj Lal
  • 231
  • 1
  • 3
  • 6
  • do you know that you can have more than one `hosts:` section in your yaml and in essence, have several playbooks using different host groups in your inventory ? – Yonsy Solis Jul 29 '19 at 23:55

2 Answers2

7

Actually, you can have more than one hosts: section per playbook. It appears that a hosts: starts a new play. See http://www.tecmint.com/use-ansible-playbooks-to-automate-complex-tasks-on-multiple-linux-servers/, for example.

Something like this works for me (ansible 2.2):

---
- hosts: localhost 
  connection: local
  roles:
    - { role: ec2,
        tag: 'master',
        instance_type: t2.2xlarge,
        count: 1
      }
  tasks:
  - shell: hostname # reports localhost

- hosts: tag_master
  tasks:
  - shell: hostname # reports instance(s) with tag 'master'

So, put hosts: at the top of each included .yml, not after the include:.

Bill Welch
  • 71
  • 1
  • 1
4

http://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts

... There are other places where variables can come from, but these are a type of variable that are discovered, not set by the user.

Facts are information derived from speaking with your remote systems...

The facts are derived from your remote hosts. So this is the reason why normally you can share facts between your different hosts when your *.yml File have several playbooks.

Now, you can access facts from other hosts if you do something like this in another part of your playbook:

{{ hostvars['server01.example.com']['ansible_eth0']['ipv4']['address'] }}
...
...
{{ hostvars[groups['servers'][0]]['ansible_eth0']['ipv4']['address'] }}

but in this case, you need to remember that you need to get the facts before you use this. Then you can setup a first part in your playbook that get all the facts for all the hosts or use fact caching for this (see: http://docs.ansible.com/ansible/playbooks_variables.html#fact-caching)

Now, If you like to share options in your playbooks maybe can be better to rethink the info to put in variables and, with this, you can "share" your variables with the same include instruction, look at this:

http://docs.ansible.com/ansible/playbooks_variables.html#variables-defined-from-included-files-and-roles

Yonsy Solis
  • 284
  • 1
  • 9
  • 2
    My question was very specific to hosts, and not variables in general. My need is to be able to create a master playbook that doesn't have a host, but each included playbook either contains a host in it, or can be supplied to it as a parameter. I can always run them as separate playbooks but then facts obtained in one do not carry forward to the next one and so there is a lot of redundancy – Pankaj Lal Apr 03 '16 at 15:51
  • read above again please: "Now, you can access facts from other hosts if you do something like this in another part of your playbook:... but in this case, you need to remember that you need to get the facts before you use this" so, yes, you can share your facts between included playbooks in your master playbook. – Yonsy Solis Apr 19 '16 at 21:03