ansible - Ansible - 循环变量
问题描述
我正在尝试使用 ansible 剧本启动应用程序。
变量 inst 获得了所有必需的软件 ID:
WEB
TS3
SQL
....
如何为所有软件 ID 创建一个循环并在所有软件上运行相同的命令?
---
- name: "start sw"
become: yes
command: "swstart {{ inst }}"
retries: 3
failed_when: ( instance_start.rc not in [ 0 ] )
解决方案
您应该首先将您的列表定义为 playbook 上的变量。之后你可以使用
with_items
像那样:
---
- hosts: all
remote_user: root
vars:
softwares:
- WEB
- TS3
- SQL
vars_files:
- /softwares.yml # if you need to import data from file
tasks:
- name: display your softwares using debug module
debug:
msg: "An item: {{ item }}"
with_items: "{{ softwares }}"
我希望这可以帮助您解决问题。
推荐阅读
- spring - 如何使用 Spring config 重新启动 ignite 服务器?
- javascript - Javascript - 使对象中的函数全局
- npm - Webpack 3:如何从编译检查中排除文件夹?
- python - 无法安装 librosa
- javascript - ForEach 在函数完成后执行
- excel - 尽管最近没有更改代码,但 Range 类的 AutoFill 方法失败
- r - 在 R 中将多个表附加到一个不同的列中
- python - Python:如何为非 NaN 或 Infinity 的浮点数定义自定义数据类型?
- git - 如何克隆远程服务器上的本地存储库?
- azure - 使用 powershell 将 Azure Webapp 插槽交换到生产插槽