ansible部署一个简单的nginx

本文介绍如何使用Ansible实现Nginx的自动化部署,包括Ansible服务端安装、管理主机配置、Playbook编写及执行流程。通过具体示例展示了如何安装Nginx、复制测试文件并重启服务。

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

                                                                                                     ansible:做一个简单nginx自动部署环境创建测试

一、ansible服务端安装

测试环境 服务端和客户端

[root@xen01 html]# cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@xen01 html]# uname -r
2.6.32-504.el6.x86_64

    ansbile轻量级IT自动部署管理工具 基于python开发,没有客户端,基于ssh 前提是你的计算机安装python,当然linux系统都自带python

yum install python-setuptools #安装python管理工具
easy_install pip
pip install ansible #安装ansible 或者aliyun等其他的开源仓库自带了ansible

二、添加要管理的主机

echo -e '[webtest]\n192.168.0.156' >> /etc/ansible/hosts    #192.168.0.156 为我测试的机器

三、写剧本playbook 用yaml语发编写

 写一个测试文件

[root@kcw tmp]# pwd
/tmp
[root@kcw tmp]# cat index.html 
<h1>Welcome to <strong>nginx</strong>TEST PAGE</h1>
[root@kcw tmp]#
vim ansible_sample_nginx.yml
---
- hosts: webtest
  remote_user: root
  tasks:
  - name: install nginx
    yum: name=http://nginx.org/packages/rhel/6/x86_64/RPMS/nginx-1.6.2-1.el6.ngx.x86_64.rpm  state=present
  - name: copy test file
    template: src=/tmp/index.html dest=/usr/share/nginx/html/   #服务器端/tmp下创建测试文件index.html
    notify:
    - restart nginx
  - name: enable nginx is running
    service: name=nginx state=started
  - name: stop iptables
    service: name=iptables state=stopped
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

ansible服务器段执行

[root@kcw ~]# ansible-playbook ansible_sample_nginx.yml  -K  #输入192.168.0.156的密码#默认我以root运行
 [WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (i.e. yum update gmp).


PLAY [webtest] **************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.0.156]

TASK: [install nginx] ********************************************************* 
ok: [192.168.0.156]

TASK: [copy test file] ******************************************************** 
ok: [192.168.0.156]

TASK: [enable nginx is running] *********************************************** 
changed: [192.168.0.156]

PLAY RECAP ******************************************************************** 
192.168.0.156              : ok=4    changed=1    unreachable=0    failed=0

四、测试

http://192.168.0.156

转载于:https://my.oschina.net/kcw/blog/379218

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值