可见,在字典中搜索关键字并与变量进行比较



我需要使用ansible自动安装docker插件。

我将使用控制台变量

运行剧本
ansible-playbook --extra-vars "SERVER=new" example.yaml

剧本在这里

---
- name: "install plugin"
hosts: localhost
gather_facts: no
vars:
servers:
new:
url: "url-new"
old:
url: "url-old"
tasks:
- name: "Debug"
shell: docker install pluginame HOST='{{ dict:new:url:url-new }}' USER='john' PASS='john'
when: '{{ SERVER }}' in dict
- name: "Debug"
shell: docker install pluginame HOST='{{ dict:new:url:url-new }}' 
when: '{{ SERVER }}' in dict

我无法理解如何比较服务器控制台变量与每个数组中的url键,我还需要在每个任务中添加一个条件,以便在url键与SERVER变量的值匹配时运行它。

帮助吗?

问候,

设置jinja2条件,根据变量SERVER的值获取url。

- name: "install plugin"
hosts: localhost
gather_facts: no
vars:
new:
url: newurl
old:
url: oldurl
tasks:
- debug:
msg: docker install pluginame HOST='{% if SERVER=="new" %}{{ new.url }}{% elif SERVER=="old" %}{{ old.url }}{% endif %}'

最新更新