1. 程式人生 > >ansible實戰與配置

ansible實戰與配置

實戰 ansible 配置

1 用playbook安裝nginx

- 思路:先在一臺機器上編譯安裝好nginx、打包,然後再用ansible去下發

先在服務端編譯nginx(服務端為chy01 192.168.212.11 客戶機為chy02 192.168.212.12)

[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包:如下操作

[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是需要一些依賴包的

[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
(安裝依賴包與相應的庫)

定義變量

[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

創建用戶並且啟動服務以及刪除壓縮包

[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調用

[root@chy01 tasks]# vim /etc/ansible/nginx_install/roles/install/tasks/main.yml 
- include: copy.yml
- include: install.yml
到此兩個roles:common和install就定義完成了

最後要定義一個入口配置文件

[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安裝的)

希望看過的童鞋多多指教,謝謝!技術分享技術分享


ansible實戰與配置