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