从以前的播放中获取Ansible_play_host?
我有一本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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用add_host模块:在第一次播放结束时,您可以添加一个任务:
您可以将值捕获在第二次播放中:
you could use add_host module: at the end of first play you add a task:
and you could trap the value in second play: