首页 > 解决方案 > 在 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)?

标签: ansibleansible-facts

解决方案


推荐阅读