从ansible playbook中的var文件中检索变量值
Retrieve variable values from var file in ansible playbook
需要一些帮助来从 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",
“根”,
“嘘00tm3!”
]
}
但根据我的要求,我必须根据剧本中的位置检索 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 }}"}
你可以这样试试吗
- name: Print
debug:
msg:
- "{{ vars[location]['onefs_host'] }}"
- "{{ api_user }}"
- "{{ api_password }}"
需要一些帮助来从 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", “根”, “嘘00tm3!” ] }
但根据我的要求,我必须根据剧本中的位置检索 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 }}"}
你可以这样试试吗
- name: Print
debug:
msg:
- "{{ vars[location]['onefs_host'] }}"
- "{{ api_user }}"
- "{{ api_password }}"