ansible实战与配置

1 用playbook安装nginx

- 思路:先在一台机器上编译安装好nginx、打包,然后再用ansible去下发

先在服务端编译nginx(服务端为chy01 192.168.212.11 客户机为chy02 192.168.212.12)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@chy01 ~] # cd /etc/ansible/
[root@chy01 ansible] # mkdir nginx_install //先创建一个nginx的安装目录
[root@chy01 ansible] # cd nginx_install/
[root@chy01 nginx_install] # mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
说明:roles目录下有两个角色,common为一些准备操作, install 为安装nginx的操作。每个角色下面又有几个目录,handlers下面是当发生改变时要执行的操作,通常用在配置文件发生改变,重启服务。files为安装时用到的一些文件,meta为说明信息,说明角色依赖等信息,tasks里面是核心的配置文件,templates通常存一些配置文件,启动脚本等模板文件,vars下为定义的变量
[root@chy01 nginx_install] # ls roles/
common   install
[root@chy01 nginx_install] # ls roles/install/
files  handlers  meta  tasks  templates  vars
[root@chy01 ~] # ls /etc/init.d/nginx 
/etc/init .d /nginx
[root@chy01 ~] # ls /usr/local/nginx/
client_body_temp/ fastcgi_temp/     logs/             sbin/             uwsgi_temp/       
conf/             html/             proxy_temp/       scgi_temp/  
[root@chy01 ~] # ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx .conf
(如上是之前编译好的nginx,在这里就不需要再次编译安装了)


编译完成后需要在服务端打包nginx包:如下操作

1
2
3
4
5
6
7
[root@chy01  local ] # tar czvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhosts" nginx/
(如上是打包nginx目录打包成nginx. tar .gz 但是在打包的过程中是不需要打包nginx.conf与vhosts的)
[root@chy01  local ] # mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
// 将压缩包移动到创建的nginx_install目录下去 
[root@chy01  local ] # cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@chy01  local ] # cp /etc/init.d/nginx  /etc/ansible/nginx_install/roles/install/templates/
启动脚本、配置文件都要放到 /etc/ansible/nginx_install/roles/install/templates 下面

 定义common的tasks,nginx是需要一些依赖包的

1
2
3
4
5
6
7
8
9
[root@chy01  local ] # cd /etc/ansible/nginx_install/roles/
[root@chy01 roles] # cd common/
[root@chy01 common] # vi tasks/main.yml 
- name: Install initializtion require software
   yum: name={{ item }} state=installed
   with_items:
     - zlib-devel
     - pcre-devel
(安装依赖包与相应的库)

定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@chy01 common] # vim /etc/ansible/nginx_install/roles/install/vars/main.yml 
nginx_user: www
nginx_port: 80
nginx_basedir:  /usr/local/nginx
// 左边是定义的名字,右边是值
[root@chy01 common] # vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml //把所有用到的文档拷贝到目标机器(定义一个子配置文件)
- name: Copy Nginx Software 
   copy: src=nginx. tar .gz dest= /tmp/nginx . tar .gz owner=root group=root
- name: Uncompression Nginx Software
   shell:  tar  zxf  /tmp/nginx . tar .gz -C  /usr/local/
- name: Copy Nginx Start Script
   template: src=nginx dest= /etc/init .d /nginx  owner=root group=root mode=0755
- name: Copy Nginx Config
   template: src=nginx.conf dest={{ nginx_basedir }} /conf/  owner=root group=root mode=0644

创建用户并且启动服务以及删除压缩包

1
2
3
4
5
6
7
8
9
10
[root@chy01 common] # vim /etc/ansible/nginx_install/roles/install/tasks/install.yml 
- name: Create Nginx User
   user: name={{ nginx_user }} state=present createhome=no shell= /sbin/nologin
- name: Start Nginx Service
   shell:  /etc/init .d /nginx  start
- name: Add Boot Start Nginx Service
   shell: chkconfig --level 345 nginx on
- name: Delete Nginx compression files
   shell:  rm  -rf  /tmp/nginx . tar .gz
//  nginx_user 调用参数;state=present 表示存在。

再创建main.yml并且把copy和install调用

1
2
3
4
[root@chy01 tasks] # vim /etc/ansible/nginx_install/roles/install/tasks/main.yml 
- include: copy.yml
- include:  install .yml
到此两个roles:common和 install 就定义完成了

最后要定义一个入口配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@chy01 tasks] # vim /etc/ansible/nginx_install/install.yml 
---
- hosts: chy02
   remote_user: root
   gather_facts: True
   roles:
     - common
     install
[root@chy01 tasks] # ansible-playbook /etc/ansible/nginx_install/install.yml
[DEPRECATION WARNING]: The use of  'include'  for  tasks has been deprecated. Use  'import_tasks'  for  static inclusions or 
'include_tasks'  for  dynamic inclusions. This feature will be removed  in  a future release. Deprecation warnings can be disabled by
  setting deprecation_warnings=False  in  ansible.cfg.
[DEPRECATION WARNING]: include is kept  for  backwards compatibility but usage is discouraged. The module documentation details 
page may explain  more  about this rationale.. This feature will be removed  in  a future release. Deprecation warnings can be 
disabled by setting deprecation_warnings=False  in  ansible.cfg.
 
PLAY [chy02] *********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [chy02]
TASK [common : Install initializtion require software] ***************************************************************************
failed: [chy02] (item=[u 'zlib-devel' , u 'pcre-devel' ]) => { "changed" false "failed" true "item" : [ "zlib-devel" "pcre-devel" ],  "msg" "No Package matching '['zlib-devel'' found available, installed or updated" "rc" : 0,  "results" : []}
     to retry, use: --limit @ /etc/ansible/nginx_install/install .retry
 
PLAY RECAP ***********************************************************************************************************************
chy02                      : ok=1    changed=0    unreachable=0    failed=1   
在执行的时候会报一个错误,这时初步感觉是因为不支持循环,解决方法需要修改一下main.yml的配置文件
[root@chy01 common] # vi tasks/main.yml 
- name: Install initializtion require software
   yum: name= "pcre-devel,zlib-devel"  state=installed
[root@chy01 common] # ansible-playbook /etc/ansible/nginx_install/install.yml
再次启动就正常了
[root@chy02 ~] # ps aux |grep nginx
root       5566  0.0  0.0  45484  1284 ?        Ss   03:05   0:00 nginx: master process  /usr/local/nginx/sbin/nginx  -c  /usr/localnginx/conf/nginx .conf
nobody     5568  0.0  0.2  47972  3920 ?        S    03:05   0:00 nginx: worker process
nobody     5569  0.0  0.2  47972  3920 ?        S    03:05   0:00 nginx: worker process
root       5683  0.0  0.0 112664   976 pts /0     R+   03:05   0:00  grep  --color=auto nginx
// 在客户机测试成功
如上需要注意一个问题:需要释放80端口,还需要保证客户机上没有安装nginx(包括用yum安装的)

希望看过的童鞋多多指教,谢谢!j_0063.gifj_0063.gif





     本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1980400,如需转载请自行联系原作者



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值