从以前的播放中获取Ansible_play_host?

发布于 2025-02-05 21:50:19 字数 1129 浏览 2 评论 0原文

我有一本Ansible Playbook,可以在一堆服务器中与管理卡进行交互,然后根据该信息制作报告。从结构上讲,它看起来像:

- hosts: all
  tasks:
    - name: do something with redfish
      uri:
         ...
      register: something

- hosts: localhost
  tasks:
    - name: produce report
      template:
        ...
      loop: "{{ SOME_LIST_OF_HOSTS }}"

最初,模板第二个任务是通过groups.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.s.. able,如果我们使用-l <​​/code>在命令行上(例如ansible-Playbook -l ollys_cluster_a ...)。在这种情况下,我希望template任务仅循环第一个播放的主机。换句话说,我想知道ansible_play_hosts_all从上一个播放中。

这就是我想到的:

- hosts: all
  gather_facts: false
  tasks:
    - delegate_to: localhost
      delegate_facts: true
      run_once: true
      set_fact:
        saved_play_hosts: "{{ ansible_play_hosts_all }}"

    ...other tasks go here...

- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg:
          play_hosts: "{{ saved_play_hosts }}"

这是最好的方法吗?

I have an ansible playbook that interacts with the management card in a bunch of servers, and then produces a report based on that information. Structurally it looks like:

- hosts: all
  tasks:
    - name: do something with redfish
      uri:
         ...
      register: something

- hosts: localhost
  tasks:
    - name: produce report
      template:
        ...
      loop: "{{ SOME_LIST_OF_HOSTS }}"

Originally, the template task in the second was looping over groups.all, but that causes a number of complications if we limited the target hosts using -l on the command line (like ansible-playbook -l only_cluster_a ...). In that case, I would like the template task to loop over only the hosts targeted by the first play. In other words, I want to know ansible_play_hosts_all from the previous play.

This is what I've come up with:

- hosts: all
  gather_facts: false
  tasks:
    - delegate_to: localhost
      delegate_facts: true
      run_once: true
      set_fact:
        saved_play_hosts: "{{ ansible_play_hosts_all }}"

    ...other tasks go here...

- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg:
          play_hosts: "{{ saved_play_hosts }}"

Is that the best way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2025-02-12 21:50:19

您可以使用add_host模块:在第一次播放结束时,您可以添加一个任务:

- name: add variables to dummy host
  add_host:
    name: "variable_holder"
    shared_variable:  "{{ saved_play_hosts }}"

您可以将值捕获在第二次播放中:

- name: second play
  hosts: localhost
  vars:
    play_hosts: "{{ hostvars['variable_holder']['shared_variable'] }}"
  tasks:
    :
    :

you could use add_host module: at the end of first play you add a task:

- name: add variables to dummy host
  add_host:
    name: "variable_holder"
    shared_variable:  "{{ saved_play_hosts }}"

and you could trap the value in second play:

- name: second play
  hosts: localhost
  vars:
    play_hosts: "{{ hostvars['variable_holder']['shared_variable'] }}"
  tasks:
    :
    :
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文