Yaron Idan February 2016

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

Answers


Pasi H February 2016

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"]}'

Post Status

Asked in February 2016
Viewed 1,315 times
Voted 5
Answered 1 times

Search




Leave an answer