Gripsiden
Gripsiden

Reputation: 497

Ansible Playbook - Import Playbook pass Host Name for each in Group - Rescursive Loop detected

please excuse my question firstly. I have searched and spent time looking at How to iterate over inventory group in ansible? and Ansible iterate over hosts in inventory group set by variable style questions.

So will try keep question simple.

Inventory Looks like: ( this is all sample adapted to explain a little better .. )

[GroupA]
host1 country=USA
host2 country=USA
host3 country=UK

So I want to pass the Group through only without the hosts , so that the playbook is executed across each host. However as part of the playbook , firstly i need to run a script on a different server ( depending on the country ) and pass the hostname to that script.

In my testing , I discovered "inventory_hostname" so that even if i didnt pass the host and I passed the Group , I could put the hostname into a variable.

When i put it all together and started with import_playbook ( because this playbook is for a different server ) , I saw :

Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ runninghost }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ v_host_name }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: recursive loop detected in template string: {{ v_host_name }}

I have a playbook where i have some conditionals based on the "country"

- hosts: "{{ v_world }}"

   vars:
    v_world: "{{ v_world }}"
    hostvalue : "{{ inventory_hostname }}"

 - import_playbook: country-stuff-USA.yml
   vars:
    v_host_name: "{{ hostvalue }}"
   when: hostvars[groups[v_world][0]]['country'] == "USA"

 - import_playbook: country-stuff-UK.yml
   vars:
    v_host_name: "{{ hostvalue }}"
   when: hostvars[groups[v_world][0]]['country']  == "UK"


country-stuff-USA.yml

---
 - hosts: "world-server-usa.world"
   vars:
     runninghost: "{{ v_host_name }}"

   roles:
   - role: world_peace-usa
     poll: 0
     vars:
      hostvalue: "{{ runninghost }}"

country-stuff-UK.yml

---
 - hosts: "world-server-usa.world"
   vars:
     runninghost: "{{ v_host_name }}"

   roles:
   - role: world_peace-uk
     poll: 0
     vars:
      hostvalue: "{{ runninghost }}"      

world_peace-uk ( main.yml )

- name: world_peace-uk
  shell: ksh /mountA/scriptuk.sh -host={{hostvalue}} 

world_peace-uk ( main.yml )

- name: world_peace-usa
  shell: ksh /mountA/scriptusa.sh -host={{hostvalue}} 

Any thoughts ? I am sure I am doing something very wrong with this combination.. but i couldnt think of a better way to use the Groups , but to pass the hostname onto a script on a different box.

Many Thanks for reading!

Upvotes: 0

Views: 2300

Answers (1)

Alassane Ndiaye
Alassane Ndiaye

Reputation: 4767

You are overcomplicating your issue, you should use delegation whenever you need to perform a task on a different host then the ones specified in the play. For example:

---

- hosts: running_hosts

  tasks:
  - name: tasks to execute on another host
    module: ...
    delegate_to: other_host

  - name: tasks to execute on the running host
    module: ...

Regarding the error you are getting, it's because v_world is referencing itself in the statement v_world: "{{ v_world }}"

Upvotes: 1

Related Questions