gpt4 book ai didi

ansible - 如何在ansible中创建一次性用户提示输入?

转载 作者:行者123 更新时间:2023-12-05 04:45:45 25 4
gpt4 key购买 nike

我有以下过于简化的 ansible 剧本:


- name: Prepare worker nodes
hosts: "{{ hosts }}"
serial:
- 1
- 3
remote_user: root
any_errors_fatal: true
vars:
hosts: nodes
reboot: false
tasks:

- pause:
prompt: "Reboot server(s) to make sure things are working during setup? (Y/n)"
echo: true
register: confirm_reboot
tags: [ untagged, hostname, netplan, firewalld ]

- set_fact:
reboot: "{{ (confirm_reboot.user_input == '' or confirm_reboot.user_input == 'Y' or confirm_reboot.user_input == 'y' ) | ternary('True', 'False') }}"
tags: [ untagged, hostname, netplan, firewalld, firewalld-install, firewalld-config ]

- debug:
msg: "{{ reboot }}"

它要求用户输入,以便它可以决定一些重启策略。当您只有一个节点时,这很好用,但是当您有多个节点时,它会提示输入每个 一个。假设您有 42 个节点——它会询问您 42 次。

我想弄清楚是否有一种简单的方法可以使提示只出现一次并在节点之间共享结果。也许我在文档中遗漏了什么?

最佳答案

给定库存

shell> cat hosts
[test]
host1
host2
host3
host4
host5

剧本

shell> cat playbook.yml
---
- hosts: test
serial:
- 1
- 3
gather_facts: false
tasks:
- pause:
prompt: "Reboot? (Y/n)"
echo: true
register: confirm_reboot
run_once: true
- debug:
msg: "Reboot {{ inventory_hostname }}"
when: confirm_reboot.user_input|lower == 'y'

按预期工作

shell> ansible-playbook -i hosts playbook.yml

PLAY [test] *********************************

TASK [pause] ********************************
[pause]
Reboot? (Y/n):
ok: [host1]

TASK [debug] ********************************
ok: [host1] =>
msg: Reboot host1

PLAY [test] *********************************

TASK [pause] ********************************
[pause]
Reboot? (Y/n):
ok: [host2]

TASK [debug] ********************************
ok: [host2] =>
msg: Reboot host2
ok: [host3] =>
msg: Reboot host3
ok: [host4] =>
msg: Reboot host4

PLAY [test] *********************************

TASK [pause] ********************************
[pause]
Reboot? (Y/n):
ok: [host5]

TASK [debug] ********************************
ok: [host5] =>
msg: Reboot host5

问:整个剧本只需要输入一次并传播到所有主机。

A:拆分剧本,例如

shell> cat playbook.yml
---
- hosts: test
gather_facts: false
tasks:
- pause:
prompt: "Reboot? (Y/n)"
echo: true
register: confirm_reboot
run_once: true

- hosts: test
serial:
- 1
- 3
gather_facts: false
tasks:
- debug:
msg: "Reboot {{ inventory_hostname }}"
when: confirm_reboot.user_input|lower == 'y'

第一个游戏的变量将在第二个游戏的所有主机之间共享

shell> ansible-playbook -i hosts playbook.yml

PLAY [test] *********************************

TASK [pause] ********************************
[pause]
Reboot? (Y/n):
ok: [host1]

PLAY [test] *********************************

TASK [debug] ********************************
ok: [host1] =>
msg: Reboot host1

PLAY [test] *********************************

TASK [debug] ********************************
ok: [host3] =>
msg: Reboot host3
ok: [host2] =>
msg: Reboot host2
ok: [host4] =>
msg: Reboot host4

PLAY [test] *********************************

TASK [debug] ********************************
ok: [host5] =>
msg: Reboot host5

关于ansible - 如何在ansible中创建一次性用户提示输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69059292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com