ansible - 在 Ansible 任务中访问其他组/主机 ip
问题描述
我想允许使用 ufw 模块从某些主机/组连接到服务器这是我的清单文件
[web]
foo.domain
bar.domain
[db]
foo.anotherdomain
[production:children]
web
db
这是我的剧本
- name: gather all hosts facts
user: root
hosts: all
- name : allow access from web group to database
ufw:
rule: allow
src: "{{ hostvars['web']['ansible_default_ipv4'].address }}"
port: 5432
comment: allow postgres from web
但我得到这个错误
fatal: [142.93.128.240]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['web}}']\" is undefined\n\nThe error appears to have been in '/home/reza/labs/testtask.yml': line 10, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - debug: msg=\"{{hostvars['web}}']['ansible_default_ipv4'].address}}\"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
我如何遍历组或单个主机的所有成员并选择变量值(在本例中为主机 ip)?
解决方案
推荐阅读
- ios - 向 iOS 钥匙串发出保存证书 -25300(未找到)如果删除,但 -25299(重复项)如果添加
- c - 为什么在每次写入之前打开文件并在每次写入后立即关闭它不是一个好主意?
- objective-c - 如何保留块中对象的值?
- android - 使用 jsoup 在谷歌文本搜索(上块)上查找第一张图片
- json - Change structure of JSON data for API POST request with Django
- python - 在向量中查找连续且相同的整数
- python - 根据元素位置编写 PDF
- javascript - 在 React APP 上将 HTML 脚本附加到 DOM
- .net - 处理 WCF 故障消息
- ios - WKWebView 的 pageCount 不正确;这可以纠正或完全删除吗?