Ansible Playbook - 为组中的每个导入 Playbook 通过主机名 - 检测到递归循环
Ansible Playbook - Import Playbook pass Host Name for each in Group - Rescursive Loop detected
首先请原谅我的问题。我已经搜索并花时间查看 and 风格的问题。
所以会尽量让问题简单化。
Inventory 看起来像:(这是所有样本改编的,解释得更好..)
[GroupA]
host1 country=USA
host2 country=USA
host3 country=UK
所以我只想在没有主机的情况下通过组,以便在每个主机上执行剧本。然而,作为剧本的一部分,首先我需要 运行 不同服务器上的脚本(取决于国家/地区)并将主机名传递给该脚本。
在我的测试中,我发现 "inventory_hostname" 这样即使我没有通过主机而通过了组,我也可以将主机名放入变量中。
当我把它们放在一起并从 import_playbook 开始时(因为这个剧本是针对不同的服务器的),我看到:
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 }}
我有一个剧本,其中有一些基于 "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}}
有什么想法吗?我确信我在使用这种组合时做错了一些事情。但是我想不出更好的方法来使用 Groups ,而是将主机名传递到不同盒子上的脚本上。
非常感谢阅读!
您的问题过于复杂了,只要您需要在与剧中指定的主机不同的主机上执行任务,就应该使用 delegation。例如:
---
- 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: ...
关于您遇到的错误,这是因为 v_world
在语句 v_world: "{{ v_world }}"
中引用了自身
首先请原谅我的问题。我已经搜索并花时间查看
所以会尽量让问题简单化。
Inventory 看起来像:(这是所有样本改编的,解释得更好..)
[GroupA]
host1 country=USA
host2 country=USA
host3 country=UK
所以我只想在没有主机的情况下通过组,以便在每个主机上执行剧本。然而,作为剧本的一部分,首先我需要 运行 不同服务器上的脚本(取决于国家/地区)并将主机名传递给该脚本。
在我的测试中,我发现 "inventory_hostname" 这样即使我没有通过主机而通过了组,我也可以将主机名放入变量中。
当我把它们放在一起并从 import_playbook 开始时(因为这个剧本是针对不同的服务器的),我看到:
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 }}
我有一个剧本,其中有一些基于 "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}}
有什么想法吗?我确信我在使用这种组合时做错了一些事情。但是我想不出更好的方法来使用 Groups ,而是将主机名传递到不同盒子上的脚本上。
非常感谢阅读!
您的问题过于复杂了,只要您需要在与剧中指定的主机不同的主机上执行任务,就应该使用 delegation。例如:
---
- 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: ...
关于您遇到的错误,这是因为 v_world
在语句 v_world: "{{ v_world }}"