【问题标题】:Retrieve variable values from var file in ansible playbook从ansible playbook中的var文件中检索变量值
【发布时间】:2022-01-12 21:23:04
【问题描述】:

需要一些帮助才能从 var 文件中检索子值。

下面是我的 var 文件

api_user: root
api_password: !vault |
          $ANSIBLE_VAULT;1.2;AES256;isi33835326238346338613761366531376662613865376263656262
          6138
Huston:
  onefs_host: 10.88.55.00

Phoenix:
  onefs_host: 10.76.52.01

下面是我的剧本

---
- name: isi_increase
  hosts: localhost
  connection: local

  vars_files:
         - isilonvars.yml

  tasks:
    - name: Print
      debug:
        msg:
          - "{{ Huston.onefs_host }}"
          - "{{ api_user }}"
          - "{{ api_password }}"

这段代码完美运行

任务 [打印] ******************************************* ****************************************************** ****************************************************** ************************************ 好的:[本地主机] => { “味精”:[ "10.88.55.00", “根”, “嘘!” ] }

但根据我的要求,我必须根据我的剧本中的位置检索 onefs_host IP。我在这里使用了额外的变量 -e "location=Huston"

    - name: Print
      debug:
        msg:
#          - "{{ wcc.onefs_host }}"
          - "{{ {{location}}.onefs_host }}"
          - "{{ api_user }}"
          - "{{ api_password }}"

我收到以下错误。

fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: {{ {{isilon_location}}.onefs_host }}"}

【问题讨论】:

标签: variables ansible


【解决方案1】:

你可以试试这个方法

- name: Print
  debug:
    msg:
    - "{{ vars[location]['onefs_host'] }}"
    - "{{ api_user }}"
    - "{{ api_password }}"

【讨论】:

  • 非常感谢它的工作。
猜你喜欢
  • 1970-01-01
  • 2019-11-22
  • 2022-01-23
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多