Yaron Idan
Yaron Idan

Reputation: 6765

Make Ansible included playbooks run on same hosts as their parent

Helllo, what is the best way to make an included playbook run on the same hosts as the playbook that called him? I've tried declaring a variable in the parent playbook with the host name and then passing it to the included playbook, but I get an error telling me that the variable is undefined.

Below is my playbook:

--- 
  # Main staging configuration playbook
- vars: 
    host_name: "stage_ansible"
  hosts: "{{ host_name }}"
  remote_user: ubuntu
  tasks:  
    - name: test connection
      ping:
      remote_user: ubuntu

- include: NginxDefinitions.yml
  vars:
    service_name: "interaction.qmerce.com"
    env_name: "stage4"
    host_name_pass: "{{ host_name }}"
...

and the error I'm receiving:

`ERROR! 'host_name' is undefined

Upvotes: 1

Views: 421

Answers (1)

Pasi H
Pasi H

Reputation: 2628

If you want to define the hosts runtime and avoid hard coding them on the playbook, you can pass the hosts as extra variables on the command line.

To do so, remove vars definition from your first play and add the following to the ansible-playbook command line:

--extra-vars host_name=localhost

or when you have multiple hosts:

--extra-vars '{"host_name":["host1","host2","host3"]}'

Upvotes: 2

Related Questions