[英]How to loop over variables in an ansible playbook
I have the following playbook that prints out a list of the plugins that are installed on three jenkins servers: 我有以下playbook打印出安装在三个jenkins服务器上的插件列表:
---
- hosts: all
remote_user: user
tasks:
- name: Obtaining a list of Jenkins Plugins
jenkins_script:
script: 'println(Jenkins.instance.pluginManager.plugins)'
url: 'http://server1.usa.com:8080/'
user: 'admin'
password: 'password'
- name: Obtaining a list of Jenkins Plugins
jenkins_script:
script: 'println(Jenkins.instance.pluginManager.plugins)'
url: 'http://server2.usa.com:8080/'
user: 'admin'
password: 'password'
- name: Obtaining a list of Jenkins Plugins
jenkins_script:
script: 'println(Jenkins.instance.pluginManager.plugins)'
url: 'http://server3.usa.com:8080/'
user: 'admin'
password: 'password'
However, this is clearly not the most efficient way to do this. 但是,这显然不是最有效的方法。 I looked into loops and variables within ansible playbooks- but I seem to be going in circles.
我查看了ansible剧本中的循环和变量 - 但我似乎是在圈子里。 Here is what I have so far:
这是我到目前为止:
- name: Obtaining a list of Jenkins Plugins
jenkins_script:
script: 'println(Jenkins.instance.pluginManager.plugins)'
url: {{ item }}
with_items:
- 'http://server1.usa.com:8080/'
- 'http://server2.usa.com:8080/'
- 'http://server3.usa.com:8080/'
user: 'admin'
password: 'password'
Here is the error message: 这是错误消息:
fatal: [server]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined\n\nThe error appears to have been in '/home/user/varspb.yml': line 5, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Obtaining a list of Jenkins Plugins\n ^ here\n"}
I'm sure it is a simple mistake, but does anyone see where I am going wrong? 我确定这是一个简单的错误,但有人看到我哪里错了吗?
If you start your value with a variable such as {{ item }}
, then it needs to be quoted with intent to interpolate a variable "{{ item }}"
. 如果您使用
{{ item }}
等变量开始使用值,则需要引用它以插入变量"{{ item }}"
。 Also, you have various formatting issues in your yaml and task block. 此外,您的yaml和任务块中存在各种格式问题。 I managed to fix your task with this:
我设法用这个来修复你的任务:
- name: Obtaining a list of Jenkins Plugins
jenkins_script:
script: 'println(Jenkins.instance.pluginManager.plugins)'
url: "{{ item }}"
user: 'admin'
password: 'password'
with_items:
- 'http://server1.usa.com:8080/'
- 'http://server2.usa.com:8080/'
- 'http://server3.usa.com:8080/'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.