【问题标题】:How do I write a playbook which gives me an option to choose between config and iteractive mode如何编写一个剧本,让我可以在配置和迭代模式之间进行选择
【发布时间】:2022-08-20 05:29:02
【问题描述】:

我正在编写一个从用户那里获取输入并处理该输入的剧本。 我也可以使用配置文件中的相同输入。现在,我如何修改剧本,以便我应该能够选择交互模式并从命令行获取输入或选择配置使用配置文件中的相同值?

类比:我们在编程语言中使用 switch 语句,基于输入选项代码执行采取不同的路径。
我们在ansible中有类似的概念吗?

  • 由于 Ansible 是一个Configuration Management 工具,并且并非旨在成为一种编程语言,因此实际上并没有这样的概念。但是,有可能实现这种行为。

标签: ansible


【解决方案1】:

Prompts 可用于获取用户输入,vars_files 可用于设置文件中的默认值。

- name: Prompt & defaults
  hosts: localhost
  vars_files:
    - defaults.yml
  vars_prompt:
    - name: username
      prompt: What is the username? Leave it empty to get the default value
      default: "{{ default_username }}"
  tasks:
    - name: Print a message
      ansible.builtin.debug:
        msg: "{{ username }}"

默认值.yml

default_username: username_from_vars

默认值将显示在方括号之间:

What is the username? Leave it empty to get the default value [username_from_vars]:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2020-03-15
    • 2022-11-04
    • 2020-05-17
    相关资源
    最近更新 更多