懒人管理员的Ansible自动化配置指南

本文详细介绍了Ansible在系统管理中的实际应用,包括软件包更新、安全配置、服务部署等自动化任务,通过具体YAML配置示例展示如何实现批量服务器管理和安全加固。

懒人管理员的Ansible指南

注意: 本博文中引用的技术和工具可能已过时,不适用于当前情况。但本文仍可作为学习机会,并可能更新或集成到现代工具和技术中。

对于懒惰的服务器和系统管理员来说,Ansible具有一些非常不错的功能,可以自动化那些无聊的功能,如更新软件包、查找过时的软件包、检查扫描等。以下是我发现非常有用的功能快速分解(全文中有用内容以粗体显示)。

首先,请务必运行最佳实践 - 此处。该软件之所以有用和惊人,原因有很多,最好总结为基于无代理SSH密钥认证的系统管理。对于注重安全的管理员,请查看“CSC 2:授权和未授权软件清单” - 此处。假设我们仅批准特定内核版本,并需要知道是否有服务器不符合批准:

1
2
3
4
5
system:~$ ansible droplets -m shell -a "dpkg -l |grep linux-image*" -u ansible -K
ii  linux-image-3.13.0-85-generic        3.13.0-85.129                amd64                         
Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-85-generic 3.13.0-85.129                amd64                        
Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP

或者,如果我们有一个版本的每个人第二喜欢的文本编辑器在最近的CVE中被识别:

1
2
3
4
5
system:~$ ansible droplets -m shell -a "dpkg -l |grep nano" -u ansible -K
11.22.33.44 | SUCCESS | rc=0 >>
ii  nano    2.2.6-1ubuntu1    amd64    small, friendly text editor inspired by Pico
33.44.55.66 | SUCCESS | rc=0 >>
ii  nano     2.2.6-1ubuntu1    amd64    small, friendly text editor inspired by Pico

上面的分块命令被认为是临时命令。让我们看一下YAML文件,了解剧本的样子。剧本可以针对所有主机或子集运行,具体取决于您的hosts.conf文件。这是一个用于更新所有软件包的apt特定.yml文件。

不可能这么简单吧?我可以一次在所有服务器上运行这个吗?是的,让我们来做吧!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
system:/etc/ansible$ ansible-playbook -l droplets roles/common/tasks/apt.yml -u ansible -K
PLAY [all] *********************************************************************
TASK [setup] ******************************************************************
ok: [11.22.33.44]
ok: [33.44.55.66]
TASK [Check if there are packages available to be installed/upgraded] **********
changed: [11.22.33.44]    ###changed here means apt-get upgrade
changed: [33.44.55.66]
TASK [Upgrade all packages to the latest version] ******************************
changed: [11.22.33.44]    ###changed here means apt-get upgrade
changed: [33.44.55.66]
TASK [Check if a reboot is required] *******************************************
ok: [11.22.33.44]
ok: [33.44.55.66]
TASK [Reboot the server] *******************************************************
skipping: [11.22.33.44]
skipping: [33.44.55.66]
PLAY RECAP *********************************************************************
11.22.33.44         : ok=4        changed=2        unreachable=0        failed=0  33.44.55.66         : ok=4        changed=2        unreachable=0        failed=0  

好的,那很好,但重启和快速检查正常运行时间呢?这些.yml文件超级简单,非常有用且极其方便。再次强调,如果您懒惰且不想碰太多东西,您可能想开始深入研究Ansible的功能。

这些命令:

1
2
system:/etc/ansible$ ansible-playbook -l droplets roles/common/tasks/reboot.yml -u ansible -K
system:/etc/ansible$ ansible-playbook -l droplets roles/common/tasks/uptime.yml -u ansible -K

接下来,假设我有一个非常标准的方式在服务器上部署ssh,并且我希望它超级简单。我的ssh.yml文件将如下所示:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
- hosts: all
  become: yes
  tasks:
   - name: configure ssh options to system spec
     template: src=/etc/ansible/roles/common/templates/ssh.conf.j2 dest=/etc/ssh/sshd_config
     notify:
        - restart ssh
        - force ssh update
     tags: ssh
   - name: be sure ssh is running and restarted
     service: name=ssh state=restarted enabled=yes
     tags: ssh

注意ssh.conf.J2 - 这是使用Ansible部署模板的标准。我还投入了大量时间创建类似的YAML文件,用于部署ntp、fail2ban、filebeat、sendmail、nginx和许多其他软件包。到目前为止,我最喜欢的一个,也是让我赢得最懒称号的一个,结合了iptables通过白名单过滤服务器访问服务,然后部署fail2ban与通用jail.local文件以阻止对各种服务的恶意认证尝试。最后,它将完全TLS启用的filebeat记录器通过防火墙端口转储到elk堆栈中!

Iptables:

1
2
3
4
5
6
7
8
9
- hosts: all  
  become: yes
  tasks:
   - name: CAREFUL ## deploy canned iptables ruleset ## CAREFUL
     copy: src=/etc/ansible/roles/common/templates/iptables.rules.j2 dest=/etc/iptables.rules
     tags: iptables
   - name: CAREFUL ## deploy firewall u+x file to enable iptables rules ## CAREFUL
     copy: src=/etc/ansible/roles/common/templates/firewall.j2 dest=/etc/network/if-up.d/firewall mode="a+x"
     tags: firewall

Fail2ban:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- hosts: all
  become: yes
  tasks:
      - name: Install fail2ban
        apt: pkg=fail2ban state=installed update-cache=yes
        register: fail2ban_install
        tags: fail2ban
        - name: Install config
        template: src=jail.local.j2 dest=/etc/fail2ban/jail.local
        notify:
          - reload fail2ban

Filebeat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- hosts: all
  become: yes
  tasks:
   - name: add apt_key for filebeat
     apt_key: url=https://packages.elasticsearch.org/GPG-KEY-elasticsearch state=present
     tags: apt_key filebeat
   - name: add apt_repo for elastic software
     apt_repository:
        repo: "deb https://packages.elastic.co/beats/apt stable main"
     tags: filebeat repo         

   - name: install filebeat
     apt: pkg=filebeat state=installed update_cache=true
     tags: filebeat
   - name: create remote directory structure /etc/pki/tls/certs
     file: path=/etc/pki/tls/certs state=directory mode=0755
     tags: filebeat tls directories 

   - name: copy over the tls verification cert for secure logging
     copy: src=/etc/ansible/roles/common/files/logstash-forwarder.crt dest=/etc/pki/tls/certs mode=0755
     notify:
        - restart filebeat
     tags: filebeat
   - name: deploy standardized internal network config via template
     template: src=/etc/ansible/roles/common/templates/filebeat.conf.j2 dest=/etc/filebeat/filebeat.yml
     tags: filebeat config template
   - name: be sure filebeat is running
     service: name=filebeat state=restarted enabled=yes
     tags: filebeat

基本上,从Linux管理角度来看,Ansible是一个绝佳选择。我们刚才经历的基本上仍然是完全临时的 - 我启动服务器,创建一个名为Ansible的新用户,添加一些密钥进行认证,并开始运行剧本和命令脚本。更好的是,如果我们使用AWS或Digital Ocean,我们可以使用角色为我们完成几乎所有工作。甚至更好的是,我们可以预烘焙密钥和用户。如果您达到这一点,请查看最后一个超链接 - 它描述了如何将所有.yml脚本烘焙到单个剧本中,用作“引导程序”。构建Linux盒子,添加一个密钥供Ansible认证,五分钟后我就有一个系统在线,完全防火墙保护,启用日志记录,按规格配置,完全打补丁,启动并为数字超空间贡献服务。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计