英文:
Ansible: hostvars keeps returning undefined even if it's defined
问题
这是我的缩短的清单文件:
worker01 ansible_host=172.20.15.78 ansible_ssh_user=service ansible_ssh_private_key_file=./variables/dev-cortex-stage/service.pri ansible_ssh_password= ansible_port=22 ansible_ssh_port=22 private_ip=172.20.15.78 worker=true ingress=false storage=true logging=true
这是播放书:
- name: "标记节点 '{{ hostvars[host]['inventory_hostname'] }}'"
delegate_to: "{{groups['controllers'][0]}}"
become: true
shell: "kubectl taint --overwrite=true nodes '{{ hostvars[host]['inventory_hostname'] }}' storage=true:NoSchedule"
when: "hostvars['inventory_hostname']['storage'] is defined"
我尝试的目标是在满足条件时标记节点(执行shell命令),但无论如何设置条件,条件都不会满足。我甚至尝试过hostvars['inventory_hostname'].storage
,但没有成功。
我做错了什么?
英文:
This is my shortened inventory file:
worker01 ansible_host=172.20.15.78 ansible_ssh_user=service ansible_ssh_private_key_file=./variables/dev-cortex-stage/service.pri ansible_ssh_password= ansible_port=22 ansible_ssh_port=22 private_ip=172.20.15.78 worker=true ingress=false storage=true logging=true
And this is the playbook:
- name: "Taint node '{{ hostvars[host]['inventory_hostname'] }}'"
delegate_to: "{{groups['controllers'][0]}}"
become: true
shell: "kubectl taint --overwrite=true nodes '{{ hostvars[host]['inventory_hostname'] }}' storage=true:NoSchedule"
when: "hostvars['inventory_hostname']['storage'] is defined"
What I'm trying to achieve is to taint (execute the shell command) when the condition is met but it is never met no matter the way i put the condition. I have even tried hostvars['inventory_hostname'].storage
but no luck.
What am I doing wrong?
答案1
得分: 1
在一个库存文件中
[test]
test.example.com storage=true
一个 最小可重现示例 的剧本
---
- 主机:测试
成为:假
收集事实:假
任务:
- 调试:
msg: " {{ inventory_hostname }} 具有存储:{{ storage }}"
when: storage is defined
将产生预期输出
任务 [调试] ******************************
ok: [test.example.com] =>
msg: 'test.example.com 具有存储:true'
英文:
With an inventory file
[test]
test.example.com storage=true
a minimal reproducible example playbook
---
- hosts: test
become: false
gather_facts: false
tasks:
- debug:
msg: "{{ inventory_hostname }} has storage: {{ storage }}"
when: storage is defined
will result into the expected output of
TASK [debug] ******************************
ok: [test.example.com] =>
msg: 'test.example.com has storage: true'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论