【发布时间】:2017-05-19 04:32:53
【问题描述】:
我想要一个 Salt 状态来管理我的 SSH 文件。这需要多个file.line 操作。我该怎么做?
更新:请参阅问题底部了解我当前的解决方法
我拥有的是这样的:
Secure SSH:
file:
- name: /etc/ssh/sshd_config
- line:
- match: "^PasswordAuthentication "
- content: "PasswordAuthentication no"
- mode: ensure
- line:
- match: "^PubkeyAuthentication "
- content: "PubkeyAuthentication yes"
- mode: ensure
- line:
- match: "^Port "
- content: "Port 8888"
- mode: ensure
service.running:
- name: sshd
- watch:
- file: /etc/ssh/sshd_config
但这失败了
Data failed to compile:
----------
No function declared in state 'file' in SLS u'xyz'
其实我的第一次尝试是这样的:
Secure SSH:
file.line:
- name: /etc/ssh/sshd_config
- match: "^PasswordAuthentication "
- content: "PasswordAuthentication no"
- mode: ensure
file.line:
- name: /etc/ssh/sshd_config
- match: "^PubkeyAuthentication "
- content: "PubkeyAuthentication yes"
- mode: ensure
file.line:
- name: /etc/ssh/sshd_config
- match: "^Port "
- content: "Port 8888"
- mode: ensure
service.running:
- name: sshd
- watch:
- file: /etc/ssh/sshd_config
但这失败了
Data failed to compile:
----------
Rendering SLS 'base:xyz' failed: Conflicting ID 'file.line'
我理解这个错误,因为每个状态函数都是一个字典键,但它看起来确实很干净。
Salt 文档在这方面没有什么帮助,因为它没有说明 anything 当您只是 也许 您想将多个内容修改到一个文件时该怎么做,而且它方便地仅在其文档中提供非常琐碎的示例。
更新:
我通过为每一行使用单独的状态来让它工作(我还将file.line 更改为file.replace,但这是另一个问题)。我认为这相当笨拙,而且不是每一步都重新加载服务吗?
Disallow SSH password authentication:
file.replace:
- name: /etc/ssh/sshd_config
- pattern: ^PasswordAuthentication .*
- repl: PasswordAuthentication no
- append_if_not_found: True
service.running:
- name: sshd
- watch:
- file: /etc/ssh/sshd_config
Allow SSH public key authentication:
file.replace:
- name: /etc/ssh/sshd_config
- pattern: ^PubkeyAuthentication .*
- repl: PubkeyAuthentication yes
- append_if_not_found: True
service.running:
- name: sshd
- watch:
- file: /etc/ssh/sshd_config
Set SSH port:
file.replace:
- name: /etc/ssh/sshd_config
- pattern: ^Port .*
- repl: Port 8888
- append_if_not_found: True
service.running:
- name: sshd
- watch:
- file: /etc/ssh/sshd_config
【问题讨论】:
标签: salt-stack