Gre*_*eg 5 ansible ansible-playbook ufw
我正在通过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"}
Run Code Online (Sandbox Code Playgroud)
整个角色如下所示:
---
- 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
Run Code Online (Sandbox Code Playgroud)
知道为什么我不能允许这些服务吗?当ssh进入服务器并运行sudo ufw allow http
等时,我能够正确地添加服务.
正如ufw 模块文档中提到的,名称(或应用程序)参数使用注册的应用程序,/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
Run Code Online (Sandbox Code Playgroud)
通常,您可以使用ufw allow application-profile
允许在 中定义的应用程序,/etc/ufw/applications.d
或者/etc/services
为 中不一定定义的内容打开 iptables /etc/ufw/applications.d
。
不幸的是,Ansible 的ufw 模块反而以这种格式构建 ufw 命令:
/usr/sbin/ufw allow from any to any app 'application-profile'
Run Code Online (Sandbox Code Playgroud)
它只使用列表/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
Run Code Online (Sandbox Code Playgroud)
然后在某处定义变量(例如您的角色默认值):
http_port: 80
https_port: 443
ssh_port: 22
Run Code Online (Sandbox Code Playgroud)
顺便说一句,您可能会注意到,我用一个键将您的字典列表简化为一个更简单的直接列表,这稍微整理了您的任务。
或者,您可以使用 Ansible 的模板模块轻松地对应用程序配置文件进行模板化。
归档时间: |
|
查看次数: |
4235 次 |
最近记录: |