如何使用用户的提示跳过播放?



我有一个剧本,里面有几个剧本,可以在一个远程服务器上安装不同的应用程序。

我需要添加交互式提示,询问用户是想安装特定的应用程序还是跳过它。

这就是我迄今为止成功构建的:

播放

- name: Telegraf setup
hosts: new_hcloud
vars_prompt:
name: "confirmation"
prompt: "Do you want to install Telegraf? Answer with 'yes', otherwise press Enter to abort"
default: "no"
private: no
gather_facts: false
remote_user: root
become: true
roles:
- telegraf
ignore_errors: true

任务

- name: Check Confirmation
fail: msg="Telegraf installation aborted!"
when: confirmation != "yes"
- name: Install the libselinux-python package for ubuntu
apt:
name: python3-selinux
state: present
- name: Download the package for ubuntu
get_url:
url: https://dl.influxdata.com/telegraf/releases/telegraf_{{ telegraf_version }}_amd64.deb
dest: /tmp/telegraf_{{ telegraf_version }}_amd64.deb
validate_certs: False

在我的任务中使用fail模块,会完全中止特定的播放,但会停止整个剧本,这就是我添加ignore_errors: true指令的原因。

问题是,这场戏并没有真正中止,而且任务无论如何都在播放。。。

我如何修改我的代码,以便能够完全跳过整个播放,但同时允许用户继续播放剩余的播放?

Q:">如何跳过整个播放,但同时允许用户继续播放剩余的播放">

A: 有条件地import_role,例如给定角色

shell> cat roles/telegraf/tasks/main.yml 
- debug:
msg: Install Telegraph
shell> cat roles/phone/tasks/main.yml 
- debug:
msg: Install Phone

和战术手册

shell> cat playbook.yml
- name: Telegraf setup
hosts: new_hcloud
gather_facts: no
vars_prompt:
name: confirmation
prompt: |
Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
default: "no"
private: no
tasks:
- import_role:
name: telegraf
when: confirmation == "yes"
- name: Phone setup
hosts: new_hcloud
gather_facts: no
vars_prompt:
name: confirmation
prompt: |
Do you want to install Phone?
Answer with 'yes', otherwise, press Enter to abort
default: "no"
private: no
tasks:
- import_role:
name: phone
when: confirmation == "yes"

给出

shell> ansible-playbook playbook.yml
Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
[no]: no
PLAY [Telegraf setup] ***********************************************************
TASK [telegraf : debug] *********************************************************
skipping: [new_hcloud]
Do you want to install Phone?
Answer with 'yes', otherwise, press Enter to abort
[no]: yes
PLAY [Phone setup] **************************************************************
TASK [phone : debug] ************************************************************
ok: [new_hcloud] => 
msg: Install Phone
PLAY RECAP **********************************************************************
new_hcloud: ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

调试

Q:"输出总是skip_reason":"条件结果为"假">

A: 最小化代码并尝试隔离问题,例如

shell> ansible --version
ansible [core 2.12.1]
...
python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
jinja version = 3.0.1
libyaml = True
vars_prompt:
name: confirmation
prompt: |
Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
default: "no"
private: no
tasks:
- debug:
var: confirmation
- debug:
var: confirmation|type_debug
- debug:
var: confirmation == "yes"

给出(表示"是"(

Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
[no]: yes
PLAY [Telegraf setup] ******************************************
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation: 'yes'
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation|type_debug: str
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation == "yes": true

或(对于"否"(

Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
[no]: no
PLAY [Telegraf setup] **********************************************
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation: 'no'
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation|type_debug: str
TASK [debug] ***************************************************
ok: [new_hcloud] => 
confirmation == "yes": false

Q:我发现的唯一方法是为每个角色使用不同的名称进行变量确认!(conftelegrapif、confphone等……(

A: 确保变量确认未在连续播放中定义。

引用交互式输入部分的注释:提示:

对于已经通过命令行--extra vars选项定义的任何变量,或者从非交互式会话(如cron或Ansible AWX(运行时,将跳过对单个vars_prompt变量的提示。请参见在运行时定义变量。

例如

- name: Telegraf setup
hosts: new_hcloud
gather_facts: no
vars_prompt:
name: confirmation
prompt: |
Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
default: "no"
private: no
tasks:
- debug:
var: confirmation
- name: Test the variable confirmation is defined or not
hosts: new_hcloud
gather_facts: no
tasks:
- debug:
var: confirmation

给出

Do you want to install Telegraf?
Answer with 'yes', otherwise, press Enter to abort
[no]: no
PLAY [Telegraf setup] **********************************************
TASK [debug] *******************************************************
ok: [new_hcloud] => 
confirmation: 'no'
PLAY [Test the variable confirmation is defined or not] ************
TASK [debug] *******************************************************
ok: [new_hcloud] => 
confirmation: VARIABLE IS NOT DEFINED!

最新更新