[root@test1 ansible]#cat nginx.yaml
---
- hosts: test
remote_user: root
vars_files:
- vars.yaml
tasks:
- name: instal nginx
yum: name=nginx
- name: copy conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: create directory
file: name=/var/nginx/html state=directory
- name: copy index.html
template: src=index.html.j2 dest=/var/nginx/html/index.html
- name: create conf.d
file: name=/etc/nginx/conf.d state=directory
- name: copy host1.conf
template: src=host1.conf.j2 dest=/etc/nginx/conf.d/{{ ansible_nodename }}.conf
when: ansible_processor_vcpus < 3
notify: restart nginx
- name: copy host2.conf
template: src=host2.conf.j2 dest=/etc/nginx/conf.d/{{ ansible_nodename }}.conf
when: ansible_processor_vcpus > 3
notify: restart nginx
- name: start nginx
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=restarted
tags: restart nginx
[root@test1 ansible]#cat vars.yaml
port1: 91
port2: 92
[root@test1 templates]#cat nginx.conf.j2
user root;
worker_processes {{ ansible_processor_vcpus+1 }};
server {
listen {{ host_port }} default_server;
[root@test1 templates]#cat index.html.j2
hello {{ ansible_nodename }}
[root@test1 templates]#cat host1.conf.j2
server {
listen {{ port1 }};
server_name 123.com;
location / {
root /var/nginx/html/;
index index.html index.htm;
}
}
[root@test1 templates]#cat host2.conf.j2
server {
listen {{ port2 }};
server_name 123.com;
location / {
root /var/nginx/html/;
index index.html index.htm;
}
}
cat hosts
[test]
192.168.31.229 host_port=81
192.168.31.71 host_port=82