ansible 使用playbook编译安装nginx
思路:
安装nginx的编译环境
下载文件、解压
编译安装
playbook:
使用yum、user、get_url、file、unarchive、chmmand这些模块
yum模块(官方):安装多个软件包
- name: ensure a list of packages installed
yum:
name: “{ { packages }}”
vars:
packages:
- httpd
- httpd-tools
user模块(官方):创建johnd、指定johnd的uid、并指定组为admin
- name: Add the user ‘johnd’ with a specific uid and a primary group of ‘admin’
user:
name: johnd
uid: 1040
group: admin
get_url模块(官方):下载并指定目录
- name: Download foo.conf
get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
mode: ‘0440’
file模块(官方):创建目录可指定权限
- name: Create a directory if it does not exist
file:
path: /etc/some_directory
state: directory
mode: ‘0755’
unarchive模块(官方):解压文件并指定目录
- name: Extract foo.tgz into /var/lib/foo
unarchive:
src: foo.tgz
dest: /var/lib/foo
chmmand模块:先执行chdir(切换目录)、再执行/usr/bin…………
- name:
command: /usr/bin/make_database.sh db_user db_name
args:
chdir: somedir/
---
#安装编译依赖环境
- name: Install and compile dependent environment
hosts: all
tasks:
- name: install yum
yum:
name: "{
{ packages }}"
vars:
packages:
- gcc
- gcc-c++
- autoconf
- automake
- zlib
- zlib-devel
- openssl
- openssl-devel
- pcre
#创建编译安装的用户
- name: Create user
user:
name: nginx
#下载nginx源码包
- name: Download the nginx source package
get_url:
url: https://nginx.org/download/nginx-1.18.0.tar.gz
dest: /mnt
#创建目录
- name: Create a directory
file:
path: /usr/local/nginx
state: directory
#解压下载的nginx源码包
- name: Unzip the nginx source package
unarchive:
src: /mnt/nginx-1.18.0.tar.gz
dest: /mnt/
#编译安装、先切换目录再执行 ./configure
- name: ./configure
command: ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx
args:
chdir: /mnt/nginx-1.18.0
- name: make
command: make
args:
chdir: /mnt/nginx-1.18.0
- name: make install
command: make install
args:
chdir: /mnt/nginx-1.18.0