【问题标题】:Ansible: Lookup variables dynamically in v2.3Ansible:在 v2.3 中动态查找变量
【发布时间】:2018-12-10 23:42:08
【问题描述】:

我有一组变量和一个任务如下。我的意图是根据用户选择的 URL 动态地进行健康检查。

vars:
  current_hostname: "{{ ansible_hostname }}"
  hc_url1: "https://blah1.com/healthcheck"
  hc_url2: "https://blah2.com/healthcheck"

tasks:
- name: Notification Msg For Healthcheck
  shell: "echo 'Performing healthcheck at the URL {{ lookup('vars', component) }} on host {{ current_hostname }}'"

在 Ansible 2.3 中运行剧本

ansible-playbook ansible_playbook.yml -i inventory -k -v --extra-vars "component=hc_url1"

错误

fatal: [hostname]: FAILED! => {"failed": true, "msg": "lookup plugin (vars) not found"}

我知道发生这种情况是因为 Ansible v2.5 中引入了查找插件“var”。有没有办法在 Ansible 2.3 中做到这一点?我想得到 {{ component }} 的值,然后是 {{ hc_url1 }}

PS - 由于组织限制,不能升级到 2.5

【问题讨论】:

    标签: variables dynamic ansible lookup ansible-2.x


    【解决方案1】:

    或者,也许您可​​以使用字典来做到这一点。

    例如,

    vars:
      current_hostname: "{{ ansible_hostname }}"
      urls:
        hc_url1: "https://blah1.com/healthcheck"
        hc_url2: "https://blah2.com/healthcheck"
    
    tasks:
    - name: Notification Msg For Healthcheck
      shell: "echo 'Performing healthcheck at the URL {{ urls[component] }} on host {{ current_hostname }}'"
    

    这样,用户提供的component 值将在字典中作为键进行查找。

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      相关资源
      最近更新 更多