在指定的群组上运行playbook?

huangapple go评论64阅读模式
英文:

Run playbook on group specified via prompt?

问题

I would like to run playbook on specified group of servers via interactive selections during play.
我想在执行期间通过交互式选择在特定的服务器组上运行playbook。

I have all groups defined in inventory file.
我在清单文件中定义了所有的组。

I would like to select the proper group via interactive session via prompt during play, e.g., via vars_prompt.
我希望在play期间通过提示进行交互式会话来选择适当的组,例如,通过 vars_prompt

From my understanding this is not possible this way, and hosts must be defined before execution in playbook hosts variable, Ansible does not support this, correct?
根据我的理解,这种方式不可行,主机必须在playbook的主机变量中定义,Ansible不支持这种方式,是吗?

Inventory file, in ini format:
清单文件,采用ini格式:

[all]
group1
group2
group3

[group1]
host01
host02

[group2]
host03
host04

[group3]
host05
host06

playbook.yml
playbook.yml

---
- hosts: group1

I made research on the web and it looks Ansible does not support this option.
我在网上进行了研究,Ansible似乎不支持这个选项。

英文:

I would like to run playbook on specified group of servers via interactive selections during play.
I have all groups defined in inventory file.
I would like to select the proper group via interactive session via prompt during play, e.g., via vars_prompt.

From my understanding this is not possible this way, and hosts must be defined before execution in playbook hosts variable, Ansible does not support this, correct?

Inventory file, in ini format:

[all]
group1
group2
group3

[group1]
host01
host02

[group2]
host03
host04


[group3]
host05
host06

playbook.yml

---
- hosts: group1

I made research on the web and it looks Ansible does not support this option.

答案1

得分: 1

- 主机: "{{ selected_group }}"
  收集事实: false

  变量提示:
    - 名称: selected_group
      提示: 你想在哪个组上执行操作?
      隐私: false

  任务:
    - 调试:
英文:

You can definitely do this because vars_prompt happens before the host selection.

So, a playbook like this is definitely something possible:

- hosts: "{{ selected_group }}"
  gather_facts: false

  vars_prompt:
    - name: selected_group
      prompt: Which group do you want to act on?
      private: false

  tasks:
    - debug:

On my setup, it would yield:

Which group do you want to act on?: nodes

PLAY [nodes] *************************************************************

TASK [debug] *************************************************************
ok: [ansible-node-2] => 
  msg: Hello world!
ok: [ansible-node-1] => 
  msg: Hello world!

huangapple
  • 本文由 发表于 2023年5月8日 02:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195683-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定