ansible-playbook 编译安装nginx

mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv

└── nginx

├── default

├── files

│ └── nginx-1.12.2.tar.gz

├── handlers

│ └── main.yml

├── meta

├── tasks

│ └── main.yml

├── templates

│ ├── index.html.j2

│ └── nginx.conf.j2

└── vars

cat nginx/tasks/main.yml

- name: copy nginx

copy: src=nginx-1.12.2.tar.gz dest=/usr/local/src/

- name: yum pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc-c++

yum: name={{ item }} state=present

with_items:

- pcre

- pcre-devel

- openssl

- openssl-devel

- zlib

- zlib-devel

- gcc-c++

- name: tar nginx

shell: chdir=/usr/local/src tar -zxf nginx-1.12.2.tar.gz

- name: install nginx

shell: chdir=/usr/local/src/nginx-1.12.2 ./configure && make && make install

- name: copy nginx.conf

template: src=nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf

- name: copy index.html

template: src=index.html.j2 dest=/usr/local/nginx/html/index.html

- name: open 80

shell: firewall-cmd --zone=public --add-port=80/tcp --permanent

notify:

- restart firewall

- name: open nginx

shell: /usr/local/nginx/sbin/nginx

cat nginx/handlers/main.yml

- name: restart firewall

service: name=firewalld state=restarted

cat nginx/templates/index.html.j2

<!DOCTYPE html>

<html>

<head>

<title>{{ ansible_all_ipv4_addresses }}</title>

</style>

</head>

<body>

<h1>This is {{ ansible_fqdn }} index page IP is {{ ansible_all_ipv4_addresses }}</h1>

</body>

</html>

cat nginx/templates/nginx.conf.j2

#user nobody;

worker_processes {{ ansible_processor_vcpus }}; //其余的没有变

cat nginx.yml

- hosts: nginx

remote_user: root

roles:

- nginx