我正在创建一个提示用户输入他/她想要的包裹的Ansible剧本,但我希望它能够不断提示用户输入包裹,直到他/她输入“完成”为止。以及如何安装用户在任务中输入的每个软件包?下面是我使用vars_prompt的代码,但是我需要帮助将其更改为我想要的功能。
---
- hosts: localhost
vars_prompt:
- name: packages
prompt: "Enter the packages you want to install. Enter 'done' to stop."
private: no
tasks:
- name: install packages user desired
yum:
name: "{{ packages }}"
state: installed
答案 0 :(得分:0)
Ansible确实不是为这种交互式操作而设计的。
一种选择是允许操作员指定以逗号分隔的软件包列表,如下所示:
---
- hosts: localhost
vars_prompt:
- name: packages
prompt: "Enter the packages you want to install, separated by commas"
private: no
tasks:
- name: install packages user desired
yum:
name: "{{ packages.split(',') }}"
state: installed
如果您确实需要单独的提示,则可以用shell脚本包装剧本,然后将结果写入到您提供的文件中,作为对剧本的输入:
#!/bin/bash
item=1
packages=()
echo "Enter packages to install. Enter 'done' to stop."
while :; do
read -p "$(( item++ )). " package
[[ $package = done ]] && break
packages+=($package)
done
(
first=1
echo '{"packages": ['
for package in "${packages[@]}"; do
[[ $first != 1 ]] && echo ","
first=0
echo "\"$package\""
done
echo ']}'
) > packages.json
ansible-playbook playbook.yml -b -e @packages.json
在这种情况下,您的剧本看起来像:
---
- hosts: localhost
gather_facts: false
tasks:
- name: install packages user desired
yum:
name: "{{ packages }}"
state: installed