使用Ansible确保文件存在,忽略任何额外的行
我试图用标准化的配置文件更新大约200台服务器上的sssd.conf文件,但是,该标准可能有一个例外。大多数服务器的配置如下所示:使用Ansible确保文件存在,忽略任何额外的行,ansible,Ansible,我试图用标准化的配置文件更新大约200台服务器上的sssd.conf文件,但是,该标准可能有一个例外。大多数服务器的配置如下所示: [domain/domainname.local] id_provider = ad access_provider = simple simple_allow_groups = unixsystemsadmins, datacenteradmins, sysengineeringadmins, webgroup default_shell = /bin/bash
[domain/domainname.local]
id_provider = ad
access_provider = simple
simple_allow_groups = unixsystemsadmins, datacenteradmins, sysengineeringadmins, webgroup
default_shell = /bin/bash
fallback_homedir = /export/home/%u
debug_level = 0
ldap_id_mapping = false
case_sensitive = false
cache_credentials = true
dyndns_update = true
dyndns_refresh_interval = 43200
dyndns_update_ptr = true
dyndns_ttl = 3600
ad_use_ldaps = True
[sssd]
services = nss, pam
config_file_version = 2
domains = domainname.local
[nss]
[pam]
但是,在某些服务器上,在简单的\u-allow\u组后面还有一行,称为简单的\u-allow\u-users,每个具有该行的服务器都配置了允许特定用户连接而不成为LDAP组的成员
我的目标是替换所有服务器上的sssd.conf文件,但不是删除这个简单的\u-allow\u-users行(如果存在的话)。我研究了lineinfle和blockinfle,但它们似乎都不能真正处理这个异常。我想我必须检查文件中是否存在行,将其存储到一个变量中,推送新文件,然后使用变量将行添加回,但我不完全确定这是否是处理它的最佳方式。有什么建议可以帮助我实现我的目标吗
谢谢 我将执行以下操作
-shell:
grep'simple_allow_users'{{sssd_conf_path}
变量:
sssd_conf_path:/etc/sssd.conf
寄存器:grep_result
-设定事实:
配置模板:|
[域名/域名.本地]
id\u provider=ad
访问\提供程序=简单
simple_allow_groups=unixsystemsadmins、datacenteradmins、sysengineeringadmins、webgroup
{%如果grep_result.stdout%中的“simple_allow_users”}
{{grep_result.stdout.rstrip()}
{%endif%}
默认shell=/bin/bash
..... 配置文件的其余部分
-副本:
内容:“{{configuration_template}}”
目标:{{sssd_conf_path}”
变量:
sssd_conf_path:/etc/sssd.conf
我使用了Zeitounator的提示以及这个问题
这就是我想到的:
*事实证明,在系统部署后,简单的\u-allow\u组正在被更改(感谢您告诉管理员们这一点,各位…/snark感谢那些乱动我的配置文件的人)
让您步入正轨的简单概述:1)
fetch
或slurp
从目标服务器获取现有文件2)查找已获取/slurped文件中的行,并注册结果(如果存在)3)将包含您的内容的模板推回到服务器,如果存在,则将该行加上该行。用你的尝试和你面临的问题来编辑你的问题,使它按你所希望的那样工作。
---
- name: Get Remote SSSD Config
become: true
slurp:
src: /etc/sssd/sssd.conf
register: slurpsssd
- name: Set simple_allow_users if exists
set_fact:
simpleallowusers: "{{ linetomatch }}"
loop: "{{ file_lines }}"
loop_control:
loop_var: linetomatch
vars:
- decode_content: "{{ slurpsssd['content'] | b64decode }}"
- file_lines: "{{ decode_content.split('\n') }}"
when: '"simple_allow_users" in linetomatch'
- name: Set simple_allow_groups
set_fact:
simpleallowgroups: "{{ linetomatch }}"
loop: "{{ file_lines }}"
loop_control:
loop_var: linetomatch
vars:
- decode_content: "{{ slurpsssd['content'] | b64decode }}"
- file_lines: "{{ decode_content.split('\n') }}"
when: '"simple_allow_groups" in linetomatch'
- name: Install SSSD Config
copy:
src: etc/sssd/sssd.conf
dest: /etc/sssd/sssd.conf
owner: root
group: root
mode: 0600
backup: yes
become: true
- name: Add simple_allow_users back to file if it existed
lineinfile:
path: /etc/sssd/sssd.conf
line: "{{ simpleallowusers }}"
insertafter: "^simple_allow_groups"
when: simpleallowusers is defined
become: true
- name: Replace simple allow groups with existing values
lineinfile:
path: /etc/sssd/sssd.conf
line: "{{ simpleallowgroups }}"
regexp: "^simple_allow_groups"
backrefs: true
when: simpleallowgroups is defined
become: true