实验:使用ansible实现编译安装nginx

博客介绍了Ansible的使用,包括开启yum的epel源安装Ansible,配置并使用拷贝key的脚本;编写Playbook时组织角色目录、准备文件、编写任务和调用角色;最后进行测试,还给出了相关参考文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装和配置ansible
  • 开启yum的epel源,安装ansible
yum -y install ansible
  • 配置
# 添加被控端的IP
vim /etc/ansible/hosts
[all]
192.168.30.10[1:4]

# 实现主控端和被控端基于key的登录验证
ssh-keygen
copy_id_main.sh

拷贝key的脚本

  • copy_id.exp,这个脚本需要安装expect软件包
#!/usr/bin/expect
set ip [lindex $argv 0]
set timeout 10
spawn ssh-copy-id root@$ip
expect {
  "yes/no" { send "yes\n";exp_continue }
  "password" { send "centos\n" }
}
expect eof
  • copy_id_main.sh
#!/bin/bash
hosts=$(ansible all --list-hosts |egrep -o '([0-9]+\.){3}[0-9]+')
for ip in $hosts;do
  copy_id.exp $ip
done
二、编写playbook
  • 组织角色目录
mkdir /root/.ansible/roles
cd /root/.ansible/rolse
mkdir -pv nginx1142/{files,tasks,templates,handlers}
  • 准备文件
cd files
wget http://nginx.org/download/nginx-1.14.2.tar.gz
  • 编写任务
# 1.
vim creat_installation_dirs.yaml
- name: "create installation dirs"
  file: path=/app/{{ item }} state=directory
  with_items:
    - nginx
    - srcs
- name: "etc dir"
  file: path=/etc/nginx/conf.d state=directory
- name: "data dir"
  file: path=/data/nginx/html state=directory
# 2.
vim prepare_source_code.yaml
- name: "copy and unpack source tar ball"
  unarchive: src=nginx.tar.gz dest=/app/srcs copy=yes
- name: "rename source dir"
  shell: mv /app/srcs/nginx-1.14.2 /app/srcs/nginx
# 3.
vim install_dependecies.yaml
- name: "install dependencies for building"
  yum: name={{ packages }} state=latest
  vars:
    packages:
      - pcre-devel
      - openssl-devel
      - zlib-devel
# 4.
vim create_user.yaml
- name: "create user for nginx"
  user: name=nginx state=present system=yes shell=/sbin/nologin
# 5.
vim build.yaml
- name: configure
  shell: ./configure --prefix=/app/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
  args:
    chdir: /app/srcs/nginx
- name: "make"
  shell: make && make install
  args:
    chdir: /app/srcs/nginx 
# 6.
vim copy_templates.yaml
- name: "copy the main configure file"
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf 
- name: "copy enviroment var"
  template: src=nginx.sh.j2 dest=/etc/profile.d/nginx.sh
- name: "copy error page"
  template: src={{ item.src }} dest=/data/nginx/html/{{ item.dest }}
  with_items:
    - { src: 404.html.j2, dest: 404.html }
    - { src: 50x.html.j2, dest: 50x.html }
# 7.
vim read_nginx_variables
- name: "source nginx.sh"
  shell: source /etc/profile.d/nginx.sh
# 8.
vim start_service.yaml
- name: "start nginx"
  shell: nginx 
# 9.
vim delete_source.yaml
- name: "clean src dir"
  shell: /bin/rm -rf /app/srcs/nginx*
# 10.
vim main.yaml
- include: delete_source.yaml
- include: creat_installation_dirs.yaml
- include: prepare_source_code.yaml
- include: install_dependecies.yaml
- include: create_user.yaml
- include: build.yaml
- include: copy_templates.yaml
- include: read_nginx_variables.yaml
- include: start_service.yaml
- include: delete_source.yaml
  • 调用角色
---
- hosts: all
  remote_user: root

  roles:
    - role: nginx1142
  • 最后的目录
tree nginx1142/
nginx1142/
|-- files
|   `-- nginx.tar.gz
|-- handlers
|-- tasks
|   |-- build.yaml
|   |-- copy_templates.yaml
|   |-- creat_installation_dirs.yaml
|   |-- create_user.yaml
|   |-- delete_source.yaml
|   |-- install_dependecies.yaml
|   |-- main.yaml
|   |-- prepare_source_code.yaml
|   |-- read_nginx_variables.yaml
|   `-- start_service.yaml
`-- templates
    |-- 404.html.j2
    |-- 50x.html.j2
    |-- nginx.conf.j2
    `-- nginx.sh.j2
三、测试结果
  • 检查
ansible-playbook -C ~/.ansible/install_nginx_from_source.yaml
  • 运行
ansible-playbook  ~/.ansible/install_nginx_from_source.yaml
参考文章
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值