6.1.2 一种预先判断是否操作的方法
最后更新于:2022-04-02 07:45:22
## 系统优化前判断是否已经操作过(过滤配置中的指定system_init字符),输出yes/no,并注册到一个列表
```
---
- name: Judge optimized
shell: if grep -q system_init {{ conf.dir }}{{ conf.name }};then echo {{ conf.tag }}_yes;else echo {{ conf.tag }}_no;fi
register: optimize_tag
loop:
- { dir: "/etc/yum.repos.d/", name: "CentOS-Base.repo", tag: "yum_centos" }
- { dir: "/etc/yum.repos.d/", name: "epel.repo", tag: "yum_epel" }
- { dir: "/etc/", name: "locale.conf", tag: "locale" }
- { dir: "/etc/security/limits.d/", name: "20-nproc.conf", tag: "nproc" }
- { dir: "/etc/", name: "limits.conf", tag: "limits" }
- { dir: "/etc/ssh/", name: "sshd_config", tag: "sshd" }
- { dir: "/etc/", name: "sysctl.conf", tag: "sysctl" }
- { dir: "/etc/profile.d/", name: "ps.sh", tag: "ps" }
- { dir: "/root/", name: ".vimrc", tag: "vimrc" }
- { dir: "/etc/fail2ban/", name: "jail.conf", tag: "fail2ban" }
- { dir: "/var/spool/cron/", name: "root", tag: "ntp" }
- { dir: "/etc/rc.d/", name: "rc.local", tag: "rc" }
loop_control:
loop_var: conf
- name: Set optimized list
vars:
op_tag_results: []
set_fact:
op_tag_results: "{{ op_tag_results + [op_tag.stdout] }}"
loop: "{{ optimize_tag.results }}"
loop_control:
loop_var: op_tag
- name: debug
debug:
var: op_tag_results
```
## 操作之前判断tag是否为yes
```
when: "'locale_yes' not in op_tag_results"
when: "'nproc_yes' not in op_tag_results"
when: "'limits_yes' not in op_tag_results"
when: "'sshd_yes' not in op_tag_results"
when: "'sysctl_yes' not in op_tag_results"
when: "'ps_yes' not in op_tag_results"
when: "'vimrc_yes' not in op_tag_results"
when: "'ntp_yes' not in op_tag_results"
when: "'fail2ban_yes' not in op_tag_results"
when: "'rc_yes' not in op_tag_results"
```
';