gpt4 book ai didi

ansible - ansible 中的换行符 - var 提示符

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

在 ansible 中,我正在尝试获取用户输入。

第一组输入要求用户从给定的输入中做出选择,而另一组提示输入用户名和密码。

任务

vars_prompt:
- name: logging_endpoint_selected
prompt : " Confirm the endpoint to forward logs.\n
1. test1 \n
2. test2 \n
* Please input either 1 or 2 based on your selection "
private: no

- name: "Username"
prompt: " Splunk software must create an administrator account during startup. Otherwise, you cannot log in.\n Create credentials for the administrator account.\n
Characters do not appear on the screen when you type in credentials\n
Please enter an adminstrator username"
private: no

- name: "Password"
prompt: " Password must contain atleast:\n * 8 total printable ASCII character(s).\n Please enter a new password"
private: yes

- name: "Confirm_Password"
prompt: " Please confirm new password"
private: yes

输出

 Confirm the endpoint to forward logs .
1. Test1
2. Test2
* Please input either 1 or 2 based on your selection : 1
Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials
Please enter an administrator username:

第一组提示和第二组提示之间没有换行。

当我们运行 ansible 脚本时,我希望 var 提示符以这种方式显示:

Confirm the endpoint to forward logs .
1. Test1
2. Test2
* Please input either 1 or 2 based on your selection : 1

You selected the option Test1

Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials
Please enter an administrator username:

我该怎么做?用户输入后,必须立即显示:You selected the option Test1

最佳答案

你应该使用和滥用 YAML 多行语法,为此我不会重写已经很完美的 question, here on StackOverflow ,也不是这个 super useful tool以更直观的方式向您展示它的所有变体。

这是一个示例剧本,其中包含两个语法,并以 assert 结尾在 pre_tasks 中确认和验证用户选择:

- hosts: localhost
gather_facts: no

vars_prompt:
- name: logging_endpoint_selected
prompt : |-
Confirm the endpoint to forward logs.
1. test1
2. test2
* Please input either 1 or 2 based on your selection
private: no

- name: "Username"
prompt: '

Splunk software must create an administrator account
during startup.
Otherwise, you cannot log in.

Create credentials for the administrator account.

Characters do not appear on the screen
when you type in credentials

Please enter an administrator username'
private: no

- name: "Password"
prompt: |-

Password must contain at least:
* 8 total printable ASCII character(s).
Please enter a new password
private: yes

- name: "Confirm_Password"
prompt: |-

Please confirm new password
private: yes

pre_tasks:
- assert:
that: logging_endpoint_selected in ['1', '2']
success_msg: >-
You selected the option Test{{ logging_endpoint_selected }}

这个剧本将像这样运行:

Confirm the endpoint to forward logs.
1. test1
2. test2
* Please input either 1 or 2 based on your selection: 1

Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials
Please enter an administrator username:

Password must contain at least:
* 8 total printable ASCII character(s).
Please enter a new password:

Please confirm new password:

PLAY [localhost] **************************************************************

TASK [assert] *****************************************************************
ok: [localhost] => changed=false
msg: You selected the option Test1

关于ansible - ansible 中的换行符 - var 提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72059464/

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