首页 > 解决方案 > 如何使用用户提示跳过播放?

问题描述

我有一本包含多个剧本的剧本,用于在单个远程服务器中安装不同的应用程序。

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

到目前为止,这是我设法构建的:

- 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指令的原因。

问题是这出戏并没有真正中止,反正任务是打出来的……

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

标签: ansible

解决方案


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

调试

问:“输出始终为 skip_reason”:“条件结果为 False”

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

问:我发现让它工作的唯一方法是为每个角色使用不同的名称来确认变量!(conftelegraf、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!

推荐阅读