102:ansible之playbook

一、playbook  就是把全部的配置寫到一個配置文件裏,直接執行這個配置文件就能夠了;html

首先定義個一個配置文件    /etc/ansible/test.ymlnginx

[root@localhost_001 ~]# vim  /etc/ansible/test.yml
cat /etc/ansible/test.yml
---                                               #註釋,表示開頭了;
- hosts: webserver                                #- host:這表示內容就開始了;   針對哪一個機器;
  remote_user: root                               #針對那些用戶;
  tasks:                                          #任務;
    - name: test_playbook
      shell: touch /tmp/fenye.txt             #用到的是shell模塊;

2:執行:   ansible-playbook      test.yml         (ansible-playbook後跟文件名就能夠了)web

[root@localhost_001 ~]# cd /etc/ansible/
[root@localhost_001 ansible]# ansible-playbook test.yml 
PLAY [webserver] ********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [localhost_03]
changed: [localhost_03]
TASK [test_playbook] ****************************************************************************
PLAY RECAP **************************************************************************************************************************
localhost_03               : ok=2    changed=1    unreachable=0    failed=0

註釋:第一行須要有三個槓,hosts參數指定了對哪些主機進行參做,若是是多臺機器能夠用逗號做爲分隔,也可使用主機組,在/etc/ansible/hosts定義;
 user參數指定了使用什麼用戶登陸遠程主機操做;
 tasks指定了一個任務,其下面的name參數一樣是對任務的描述,在執行過程當中會打印出來,shell是ansible模塊名字shell

2:playbook的變量vim

下面在001(129)寫一個建立用戶的playbook。bash

[root@localhost_001 ansible]# vim create_user.yml
---
- name: create_user                  #name是對playbook作一個描述,後面執行中會打印;
  hosts: localhost_002               #針對哪一個主機或者組;
  user: root                         #以那個用戶的身份來執行;
  gather_facts: false                #是是否移動setup模塊收集系統信息(IP地址主機信息),false表示不收集;
  vars:
    - user: "test"                 #此處是用vars後引出一個變量,用於在下面引用;
  tasks:
    - name: create user      
      user: name="{{ user }}"     調用了user模塊,name是其裏面的一個參數;user表示上面的變量;
       
說明:name參數對該playbook實現的功能作一個概述,後面執行過程當中,會打印 name變量的值 ,能夠省略;gather_facts參數指定了在如下任務部分執行前,是否先執行setup模塊獲取主機相關信息,這在後面的task會使用到setup獲取的信息時用到;vars參數,指定了變量,這裏指字一個user變量,其值爲test ,須要注意的是,變量值必定要用引號引住;user提定了調用user模塊,name是user模塊裏的一個參數,而增長的用戶名字調用了上面user變量的值。

在001(129)上執行後,以下內容:服務器

[root@localhost_001 ansible]# ansible-playbook create_user.yml 
PLAY [create_user] ******************************************************************************************************************
TASK [create user] ******************************************************************************************************************
changed: [localhost_002]
PLAY RECAP **************************************************************************************************************************
localhost_002              : ok=1    changed=1    unreachable=0    failed=0  

在002(130)上查看該用戶;
[root@localhost_002 ~]# id test
uid=1003(test) gid=1003(test) 組=1003(test)

而若是執行時,若是用戶存在的話,則執行以下;不變動:  changed=0    表示不變動ide

[root@localhost_001 ansible]# ansible-playbook create_user.yml 
PLAY [create_user] ******************************************************************************************************************
TASK [create user] ******************************************************************************************************************
ok: [localhost_002]
PLAY RECAP **************************************************************************************************************************
localhost_002              : ok=1    changed=0    unreachable=0    failed=0

刪除用戶;user: name="{{ user }}" state=absent remove=yesui

[root@localhost_001 ansible]# cat del_user.yml 
---
- name: create_user
  hosts: localhost_002
  user: root
  vars:
   - user: "test"
  gather_facts: false
   tasks:
    - name: create_user
    user: name="{{ user }}" state=absent remove=yes

 

3:pllaybook循環this

vi /etc/ansible/while.yml //加入以下內容
---
- hosts: testhost
  user: root
  tasks:
    - name: change mode for files
      file: path=/tmp/{{ item }} mode=600
      with_items:
        - 1.txt
        - 2.txt
        - 3.txt
 說明: with_items爲循環的對象
 執行 ansible-playbook while.yml

4:playbook的條件判斷

vi /etc/ansible/when.yml //加入以下內容
---
- hosts: testhost
  user: root
  gather_facts: True
  tasks:
    - name: use when
      shell: touch /tmp/when.txt
      when: ansible_ens33.ipv4.address == "172.7.15.114「
 說明:ansible aming-02 -m setup 能夠查看到全部的facter信息

5:Ansible playbook中的handlers

執行task以後,服務器發生變化以後要執行的一些操做,好比咱們修改了配置文件後,須要重啓一下服務 vi /etc/ansible/handlers.yml//加入以下內容
---
- name: handlers test
  hosts: aming-02
  user: root
  tasks:
    - name: copy file
      copy: src=/etc/passwd dest=/tmp/aaa.txt
      notify: test handlers
  handlers:
    - name: test handlers
      shell: echo "111111" >> /tmp/aaa.txt
 說明,只有copy模塊真正執行後,纔會去調用下面的handlers相關的操做。也就是說若是1.txt和2.txt內容是同樣的,並不會去執行handlers裏面的shell相關命令。 這種比較適合配置文件發生更改後,重啓服務的操做。

6:安裝nginx:使用源碼安裝,定製選項,使用ansible來作:用來擴容後,把已經存在模板,配置文件拷貝過去;

首先在一臺機器上安裝好nginx,而後把nginx打包,而後分發到各個機器;

1:在/etc/ansible/目錄下再建立一個目錄;

[root@localhost_001 ~]# cd /etc/ansible/
[root@localhost_001 ansible]# mkdir nginx_install
[root@localhost_001 ansible]# cd nginx_install/
[root@localhost_001 nginx_install]# mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
[root@localhost_001 nginx_install]# ls
roles
[root@localhost_001 nginx_install]# ls roles/
common  install

註釋: mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}這條命令表示在 roles下建立 common和install目錄,而後在這兩個目錄下分別建立handlers    files  meta  tasks  templates  vars等目錄;

roles的目錄狀況:有common和install兩個角色;

common:表示作一些準備操做;

install:表示安裝nginx的操做;

這兩個角色下的目錄狀況以下;

handlers:表示當發生改變時要執行的操做,一般用在配置文件發生改變,重啓服務;

files:表示安裝時用到的一些文件;

meta:爲角色信息,說明角色依賴信息;

tasks:核心依賴配置文件;

template:配置文件,啓動腳本等文件;能夠針對系統版原本作不一樣的變量在裏面;

vars:表示定義的變量;

(1):首先在一臺機器上編譯好nginx,並指定nginx的目錄,  啓動腳本   及配置文件;

[root@localhost_001 install]# ls /usr/local/nginx/
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost_001 install]# ls /etc/init.d/nginx 
/etc/init.d/nginx
[root@localhost_001 install]# ls /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf

(2):把nginx打包,並放到在/etc/ansible/nginx_install/roles/install/files

[root@localhost_001 local]# tar zcvf nginx.tar.gz --exclude "nginx.conf" --exclude "vhosts" nginx^C
[root@localhost_001 local]# mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
[root@localhost_001 local]# ls !$
ls /etc/ansible/nginx_install/roles/install/files/
nginx.tar.gz

(3):把nginx啓動腳本和配置文件放到/etc/ansible/nginx_install/roles/install/template

[root@localhost_001 local]# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
[root@localhost_001 local]# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

(4):定義common下的tasks,nginx須要依賴的一些包;

[root@localhost_001 local]# cd  /etc/ansible/nginx_install/roles
[root@localhost_001 roles]# vim common/tasks/main.yml
- name: Install initializtion require software
  yum: name="pcre-devel,zlib-devel" state=installed

(5):定義common下的vars變量;能夠自定義變量,也能夠指定某臺機器用某個用戶;

[root@localhost_001 roles]# vim common/vars/main.yml
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx

(7):把配置文件拷貝到目標機器上;

[root@localhost_001 roles]# vim 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

(8):接下來會創建啓動服務,刪除配置文件;

[root@localhost_001 roles]# vim install/tasks/install.yml
cat 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

(9):在install目錄下建立main.yml,把copy和install調用;

[root@localhost_001 roles]# vim install/tasks/main.yml
cat install/tasks/main.yml
- include: copy.yml
- include: install.yml

至此兩個common和install定義完成了;

(10):接下來定義一個接口配置文件;

[root@localhost_001 nginx_install]# vim install.yml
cat install.yml
- hosts: testhost
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

而後執行:ansible-playbook  /etc/ansible/nginx_install/install.yml

[root@localhost_001 ansible]# ansible-playbook /etc/ansible/nginx_install/install.yml 

PLAY [testhost] *********************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [localhost_002]
ok: [127.0.0.1]

TASK [common : Install initializtion require software] ******************************************************************************
ok: [127.0.0.1]
ok: [localhost_002]

TASK [install : Copy Nginx Software] ************************************************************************************************
changed: [localhost_002]
changed: [127.0.0.1]

TASK [install : Uncompression Nginx Software] ***************************************************************************************
 [WARNING]: Consider using the unarchive module rather than running tar.  If you need to use command because unarchive is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.

changed: [127.0.0.1]
changed: [localhost_002]

TASK [install : Copy Nginx Start Script] ********************************************************************************************
changed: [localhost_002]
ok: [127.0.0.1]

TASK [install : Copy Nginx Config] **************************************************************************************************
ok: [127.0.0.1]
changed: [localhost_002]

TASK [install : Create Nginx User] **************************************************************************************************
changed: [localhost_002]
changed: [127.0.0.1]

TASK [install : Start Nginx Service] ************************************************************************************************
changed: [127.0.0.1]
changed: [localhost_002]

TASK [install : Add Boot Start Nginx Service] ***************************************************************************************
changed: [localhost_002]
changed: [127.0.0.1]

TASK [install : Delete Nginx compression files] *************************************************************************************
 [WARNING]: Consider using the file module with state=absent rather than running rm.  If you need to use command because file is
insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.

changed: [127.0.0.1]
changed: [localhost_002]

PLAY RECAP **************************************************************************************************************************
127.0.0.1                  : ok=10   changed=6    unreachable=0    failed=0   
localhost_002              : ok=10   changed=8    unreachable=0    failed=0

此時在002這臺機器查詢nginx已經啓動;

[root@localhost_002 ~]# ls /usr/local/nginx/
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost_002 ~]# ps aux |grep nginx
root       2640  0.0  0.1  46392  1264 ?        Ss   16:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     2641  0.0  0.3  48132  3876 ?        S    16:06   0:00 nginx: worker process
nobody     2642  0.0  0.3  48132  3876 ?        S    16:06   0:00 nginx: worker process
root       2772  0.0  0.0 112720   972 pts/0    R+   16:09   0:00 grep --color=auto nginx
相關文章
相關標籤/搜索