Setup nginx.
Directory tree.
.
├── README.md
├── ansible.cfg
├── etc
│ └── nginx
│ ├── centos
│ │ ├── conf.d
│ │ │ ├── your-domain-example.com.conf
│ │ ├── html
│ │ │ └── index.html
│ │ ├── mime.types
│ │ └── nginx.conf
│ └── ubuntu
│ ├── conf.d
│ │ └── your-domain-example.com.conf
│ ├── html
│ │ └── index.html
│ ├── mime.types
│ └── nginx.conf
├── group_vars
│ ├── all.yml
│ ├── webserver_centos.yml
│ └── webserver_ubuntu.yml
├── inventory
└── webservers.yml
[webservers:children]
webserver_ubuntu
webserver_centos
[webserver_ubuntu]
ubuntu ansible_host=10.10.10.10 ansible_port=22
[webserver_centos]
centos ansible_host=10.10.10.11 ansible_port=22
all.yml
sets common variables.
# Common settings
become: yes
ansible_user: root
# Private_key is saved in local host only!
ansible_ssh_private_key_file: ""
# The path of nginx configuraiton files
nginx_conf_src_path: etc/nginx/ubuntu
webserver_ubuntu.yml
is webservers
host's children.
This role does NOT need to define any variables.
webserver_ubuntu.yml
is webservers
host's children.
This role does NOT need to define any variables.
- hosts: webservers
become: yes
module_defaults:
apt:
cache_valid_time: 86400
roles:
- user
This role copy some config files(nginx.conf
, mime.types
, conf.d/*
and html/*
) in /usr/loca/etc/nginx/
.
So when you want to copy these files to ubuntu
(ubuntu is inventory's host name),
you should make ./etc/nginx/ubuntu/{nginx.conf, mime.types, conf.d/, html/}
.
etc/nginx/ubuntu/
├── conf.d
│ ├── your-domain-example.com.conf
├── html
│ └── index.html
├── mime.types
└── nginx.conf
The exmaple of nginx.conf.
user nginx;
worker_processes auto;
pid /var/log/nginx/nginx.pid;
events {
worker_connections 1024;
}
http {
lua_package_path "/usr/local/lib/lua/?.lua;;";
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
user
:nginx
pid
:/var/log/nginx/nginx.pid
lua_package_path
:/usr/local/lib/lua/?.lua;;
DryRun
ansible-playbook -i inventory --private-key="~/.ssh/your_private_key" -CD webservers.yml --tags nginx
Apply
ansible-playbook -i inventory --private-key="~/.ssh/your_private_key" -D webservers.yml --tags nginx
Nothing in current.
Start VM instance kind of Ubuntu 18.04, Ubuntu 20.04, Ubuntu 22.04, CentOS7 and CentOS8 and
run ansible-playbook command such as ansible-playbook -i inventory -CD playbook.yml --private-key ~/.ssh/your_private_key --tags nginx
.
- Not support CentOS9
- We give up on using
molecule
because sometime molecule are broken and don't work correctly.