首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Ansible Playbook -为组中的每个成员导入pass主机名-检测到Rescursive Loop

Ansible Playbook -为组中的每个成员导入pass主机名-检测到Rescursive Loop
EN

Stack Overflow用户
提问于 2019-06-27 05:29:24
回答 1查看 1.4K关注 0票数 0

请先原谅我的问题。我已经搜索并花时间查看了How to iterate over inventory group in ansible?Ansible iterate over hosts in inventory group set by variable风格的问题。

所以我会尽量保持问题的简单性。

清单看起来像这样:(这是为了更好地解释而改编的所有样本。)

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

因此,我希望只在没有主机的情况下传递Group,以便在每个主机上执行攻略。然而,作为剧本的一部分,首先我需要在不同的服务器上运行一个脚本(取决于国家),并将主机名传递给该脚本。

在我的测试中,我发现了"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}} 

有什么想法吗?我确信我在这个组合中做了一些非常错误的事情..但是我想不出更好的方法来使用组,除了将主机名传递到不同机器上的脚本中。

非常感谢您的阅读!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-27 06:49:34

您的问题过于复杂了,无论何时您需要在不同于剧本中指定的主机上执行任务时,都应该使用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_worldv_world: "{{ v_world }}"语句中引用了自己

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56781129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档