Skip to content

Commit 8fc20e6

Browse files
committed
Fixes #283: Add security code snippets to new playbook for chapter 11.
1 parent fe7becf commit 8fc20e6

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-1
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ env:
8484
# - playbook: orchestration.yml
8585
# distro: ubuntu2004
8686

87+
- playbook: security.yml
88+
distro: centos8
89+
8790
- playbook: solr.yml
8891
distro: ubuntu2004
8992

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Here is an outline of all the examples contained in this repository, by chapter:
6363

6464
### Chapter 11
6565

66-
- N/A
66+
- [`security`](security/): A playbook containing many security automation tasks to demonstrate how Ansible helps automate security hardening.
6767

6868
### Chapter 12
6969

security/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Security Examples for Ansible
2+
3+
This directory contains playbooks used for security hardening with Ansible, and is meant as a companion to the examples and text in chapter 11 of [Ansible for DevOps](https://www.ansiblefordevops.com).
4+
5+
## Usage
6+
7+
TODO.
8+
9+
## About the Author
10+
11+
This project was created by [Jeff Geerling](https://www.jeffgeerling.com/) as an example for [Ansible for DevOps](https://www.ansiblefordevops.com/).

security/Vagrantfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
Vagrant.configure("2") do |config|
5+
config.vm.box = "geerlingguy/centos8"
6+
config.vm.synced_folder '.', '/vagrant', disabled: true
7+
config.ssh.insert_key = false
8+
9+
config.vm.provider "virtualbox" do |v|
10+
v.name = "security"
11+
v.memory = 1024
12+
v.cpus = 1
13+
end
14+
15+
# Provisioning configuration for Ansible.
16+
config.vm.provision "ansible" do |ansible|
17+
ansible.playbook = "main.yml"
18+
end
19+
end

security/main.yml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
- hosts: all
3+
become: true
4+
5+
handlers:
6+
- name: restart ssh
7+
service: name=sshd state=restarted
8+
9+
tasks:
10+
# Use secure and encrypted communication.
11+
- name: Allow sshd to listen on tcp port 2849.
12+
seport:
13+
ports: 2849
14+
proto: tcp
15+
setype: ssh_port_t
16+
state: present
17+
18+
- name: Update SSH configuration to be more secure.
19+
lineinfile:
20+
dest: /etc/ssh/sshd_config
21+
regexp: "{{ item.regexp }}"
22+
line: "{{ item.line }}"
23+
state: present
24+
validate: 'sshd -t -f %s'
25+
with_items:
26+
- regexp: "^PasswordAuthentication"
27+
line: "PasswordAuthentication no"
28+
- regexp: "^PermitRootLogin"
29+
line: "PermitRootLogin no"
30+
- regexp: "^Port"
31+
line: "Port 2849"
32+
notify: restart ssh
33+
34+
# User account configuration.
35+
- name: Add a deployment user.
36+
user:
37+
name: johndoe
38+
state: present
39+
40+
# Disable root login and use `sudo`.
41+
- name: Add sudo rights for deployment user.
42+
lineinfile:
43+
dest: /etc/sudoers
44+
regexp: '^johndoe'
45+
line: 'johndoe ALL=(ALL) NOPASSWD: ALL'
46+
state: present
47+
validate: 'visudo -cf %s'
48+
49+
# Remove unused software, open only required ports.
50+
- name: Remove unused packages.
51+
package:
52+
name:
53+
- nano
54+
- sendmail
55+
state: absent
56+
57+
# File permissions.
58+
- name: Configure the permissions for the messages log.
59+
file:
60+
path: /var/log/messages
61+
owner: root
62+
group: root
63+
mode: 0600
64+
65+
# Automating updates for RHEL systems.
66+
- name: Install dnf-automatic.
67+
yum:
68+
name: dnf-automatic
69+
state: present
70+
71+
- name: Ensure dnf-automatic is running and enabled on boot.
72+
service:
73+
name: dnf-automatic-install.timer
74+
state: started
75+
enabled: yes
76+
77+
# Configuring a firewall with `firewalld` on RHEL.
78+
- name: Ensure firewalld is running.
79+
service:
80+
name: firewalld
81+
state: started
82+
83+
- name: Configure open ports with firewalld.
84+
firewalld:
85+
state: "{{ item.state }}"
86+
port: "{{ item.port }}"
87+
zone: external
88+
immediate: yes
89+
permanent: yes
90+
with_items:
91+
- { state: 'enabled', port: '22/tcp' }
92+
- { state: 'enabled', port: '80/tcp' }
93+
- { state: 'enabled', port: '123/udp' }
94+
95+
# Monitor logins and block suspect IP addresses.
96+
- name: Ensure EPEL repo is present.
97+
yum:
98+
name: epel-release
99+
state: present
100+
when: ansible_os_family == 'RedHat'
101+
102+
- name: Install fail2ban (RedHat).
103+
yum:
104+
name: fail2ban
105+
state: present
106+
enablerepo: epel
107+
when: ansible_os_family == 'RedHat'
108+
109+
- name: Install fail2ban (Debian).
110+
apt:
111+
name: fail2ban
112+
state: present
113+
when: ansible_os_family == 'Debian'
114+
115+
- name: Ensure fail2ban is running and enabled on boot.
116+
service:
117+
name: fail2ban
118+
state: started
119+
enabled: yes
120+
121+
# Use SELinux (Security-Enhanced Linux).
122+
- name: Install Python SELinux library.
123+
yum:
124+
name: python3-libselinux
125+
state: present
126+
127+
- name: Ensure SELinux is enabled in `targeted` mode.
128+
selinux:
129+
policy: targeted
130+
state: enforcing
131+
132+
- name: Ensure httpd can connect to the network.
133+
seboolean:
134+
name: httpd_can_network_connect
135+
state: yes
136+
persistent: yes

tests/security.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
# Security test.
3+
- import_playbook: ../security/main.yml

0 commit comments

Comments
 (0)