Skip to content

Commit 69b2384

Browse files
committed
ansible jinja template
1 parent b0e6c36 commit 69b2384

File tree

9 files changed

+180
-28
lines changed

9 files changed

+180
-28
lines changed

part-10-ansible-imports-roles/ansible-import-roles-playbook.yml

-17
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,6 @@
2626

2727

2828

29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
4629

4730

4831

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Run playbook
2+
3+
```bash
4+
ansible-playbook --inventory inventory/ansible-jinja2-template-playbook/hosts ansible-jinja2-template-playbook.yml
5+
```
6+
7+
8+
## How to find apache is installed or not
9+
10+
```
11+
type -a apache2
12+
```
13+
14+
15+
## SSH to EC2 instance
16+
17+
```
18+
ssh -i /Users/rahulwagh/.ssh/aws_ec2_terraform [email protected]
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
- name: Example Ansible playbook for Handlers
3+
hosts: all
4+
become: yes
5+
remote_user: ubuntu
6+
roles:
7+
- install-lighttpd
8+
- install-apache
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[default]
2+
ec2-18-196-64-247.eu-central-1.compute.amazonaws.com ansible_ssh_private_key_file=/Users/rahulwagh/.ssh/aws_ec2_terraform
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: Install Apache web server
3+
apt:
4+
name: apache2
5+
state: latest
6+
update_cache: yes
7+
8+
- name: Start Apache2 service
9+
service:
10+
name: apache2
11+
state: started
12+
enabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
- name: Install lighttpd web server
3+
become: yes
4+
apt:
5+
name: lighttpd
6+
state: latest
7+
update_cache: yes
8+
9+
#- name: Update Lighttpd configuration
10+
# lineinfile:
11+
# path: /etc/lighttpd/lighttpd.conf
12+
# regexp: '^server.port = 80'
13+
# line: 'server.port = 9091'
14+
15+
- name: Generate configuration file
16+
template:
17+
src: myconfig.j2
18+
dest: /etc/lighttpd/lighttpd.conf
19+
owner: root
20+
group: root
21+
mode: '0644'
22+
vars:
23+
server_port: 80
24+
25+
- name: Start lighttpd service
26+
become: yes
27+
service:
28+
name: lighttpd
29+
state: started
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
server.modules = (
2+
"mod_indexfile",
3+
"mod_access",
4+
"mod_alias",
5+
"mod_redirect",
6+
)
7+
8+
server.document-root = "/var/www/html"
9+
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
10+
server.errorlog = "/var/log/lighttpd/error.log"
11+
server.pid-file = "/run/lighttpd.pid"
12+
server.username = "www-data"
13+
server.groupname = "www-data"
14+
server.port = {{ server_port }}
15+
16+
# strict parsing and normalization of URL for consistency and security
17+
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails
18+
# (might need to explicitly set "url-path-2f-decode" = "disable"
19+
# if a specific application is encoding URLs inside url-path)
20+
server.http-parseopts = (
21+
"header-strict" => "enable",# default
22+
"host-strict" => "enable",# default
23+
"host-normalize" => "enable",# default
24+
"url-normalize-unreserved"=> "enable",# recommended highly
25+
"url-normalize-required" => "enable",# recommended
26+
"url-ctrls-reject" => "enable",# recommended
27+
"url-path-2f-decode" => "enable",# recommended highly (unless breaks app)
28+
#"url-path-2f-reject" => "enable",
29+
"url-path-dotseg-remove" => "enable",# recommended highly (unless breaks app)
30+
#"url-path-dotseg-reject" => "enable",
31+
#"url-query-20-plus" => "enable",# consistency in query string
32+
)
33+
34+
index-file.names = ( "index.php", "index.html" )
35+
url.access-deny = ( "~", ".inc" )
36+
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
37+
38+
compress.cache-dir = "/var/cache/lighttpd/compress/"
39+
compress.filetype = ( "application/javascript", "text/css", "text/html", "text/plain" )
40+
41+
# default listening port for IPv6 falls back to the IPv4 port
42+
## Use ipv6 if available
43+
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
44+
include_shell "/usr/share/lighttpd/create-mime.conf.pl"
45+
include "/etc/lighttpd/conf-enabled/*.conf"
46+
47+
#server.compat-module-load = "disable"
48+
server.modules += (
49+
"mod_compress",
50+
"mod_dirlisting",
51+
"mod_staticfile",
52+
)

part-9-ansible-conditionals/roles/custom-role/tasks/main.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@
66
state: absent
77
when: install_apache_flag
88

9-
## Conditionals based on ansible_facts
10-
- name: How to use ansible_facts
11-
debug:
12-
var: ansible_facts['distribution']
13-
#var: ansible_facts['distribution_major_version']
14-
#var: ansible_facts['kernel_version']
15-
#var: ansible_facts
16-
when: ansible_facts['os_family'] == "Debian"
17-
189
## Putting multiple condition using ansible_facts
1910
- name: Combine conditions using ansible facts
2011
debug:
@@ -43,4 +34,13 @@
4334
- name: Run with items greater than 5
4435
ansible.builtin.command: echo {{ item }}
4536
loop: [ 0, 2, 4, 6, 8, 10 ]
46-
when: item > 5
37+
when: item > 5
38+
39+
## Conditionals based on ansible_facts
40+
- name: How to use ansible_facts
41+
debug:
42+
var: ansible_facts['distribution']
43+
#var: ansible_facts['distribution_major_version']
44+
#var: ansible_facts['kernel_version']
45+
#var: ansible_facts
46+
when: ansible_facts['os_family'] == "Debian"
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
---
2-
install_apache_flag: false
2+
install_apache_flag: true
3+
4+
5+

0 commit comments

Comments
 (0)