【问题标题】:Ansible ufw module ERROR: Could not find a profile matching 'xxxxx'Ansible ufw 模块错误:找不到匹配“xxxxx”的配置文件
【发布时间】:2016-05-17 06:07:01
【问题描述】:

我正在通过 Ansible 设置 UFW 规则。我能够安装它,启动它并拒绝一切。然后我尝试允许来自 http、https 和 ssh 的连接。所有为这些项目添加允许的尝试都会遇到如下错误:

failed: [lempy1] (item={u'service': u'http'}) => {"failed": true, "item": {"service": "http"}, "msg": "ERROR: Could not find a profile matching 'http'\n"}
failed: [lempy1] (item={u'service': u'https'}) => {"failed": true, "item": {"service": "https"}, "msg": "ERROR: Could not find a profile matching 'https'\n"}
failed: [lempy1] (item={u'service': u'ssh'}) => {"failed": true, "item": {"service": "ssh"}, "msg": "ERROR: Could not find a profile matching 'ssh'\n"}

整个角色是这样的:

tasks/main.yml

     ---
    - name: Install ufw
      apt: name=ufw state=present
      tags:
        - security

    - name: Allow webservery things
      ufw:
        rule: allow
        name: '{{item.service}}'
      with_items:
        - service: http
        - service: https
        - service: ssh
      tags:
        - security

    - name: Start ufw
      ufw: state=enabled policy=deny
      tags:
        - security

知道为什么我不能允许这些服务吗?当 ssh 进入服务器并运行 sudo ufw allow http 等时,我能够正确添加服务。

【问题讨论】:

  • 会不会与单引号传输的规则名称有关?我知道 YAML 会强制您引用 {{}},但您可以尝试将 '{{item.service}}' 替换为 "{{item.service}}" 并检查结果。

标签: ansible ansible-playbook ufw


【解决方案1】:

ufw module docs 中所述,名称(或应用)参数使用在/etc/ufw/applications.d 中注册的应用程序,这些应用程序具有INI 格式,如下所示:

[CUPS]
title=Common UNIX Printing System server
description=CUPS is a printing system with support for IPP, samba, lpd, and other protocols.
ports=631

通常您可以使用ufw allow application-profile 来允许在/etc/ufw/applications.d/etc/services 中定义的应用程序为/etc/ufw/applications.d 中未定义的内容打开iptables。

不幸的是,Ansible 的 ufw module 改为以这种格式构建 ufw 命令:

/usr/sbin/ufw allow from any to any app 'application-profile'

使用/etc/ufw/applications.d 列表,不会读取/etc/services

在您的情况下,您可以简单地指定端口,因为这些是众所周知的,可能使用命名变量来进一步解释您的 Ansible 代码:

- name: Allow webservery things
  ufw:
    rule: allow
    port: '{{ item }}'
  with_items:
    - '{{ http_port }}'
    - '{{ https_port }}'
    - '{{ ssh_port }}'
  tags:
    - security

然后在某处定义变量(比如你的角色默认值):

http_port: 80
https_port: 443
ssh_port: 22

顺便说一句,您可能想注意到我用一个键将您的字典列表简化为一个更简单的直接列表,从而稍微整理您的任务。

或者,您可以使用 Ansible 的 template module 轻松模板化应用程序配置文件。

【讨论】:

  • 所以我仍然得到找不到匹配“80”的配置文件。对于我尝试分配的所有其他端口也是如此。错误看起来像:{“失败”:true,“item”:80,“msg”:“错误:找不到匹配'80'\n”的配置文件}。有任何想法吗?我的任务现在看起来与您在此处列出的完全一样。我想知道模板是否是最好的选择。
  • 抱歉,应该是端口而不是名称。当我将您的代码复制到我的答案中时忘记编辑它
  • 想办法解决这个问题,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多