Ansible

1.1    基礎

1.1.1   簡介

ansible基於python開發,集合衆多運維工具(puppet ,cfengine ,chef ,func ,fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。其基於模塊工做,自己沒有批量部署能力,真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。包括:node

1)connection plugins:負責和被監控端實現通訊;python

2)host inventory:指定操做主機,是一個配置文件裏面定義監控的主機;mysql

3)modules:核心模塊、command模塊、自定義模塊;nginx

4)plugins:藉助插件完成記錄日誌郵件等功能;web

5)playbook:可實現一次性運行多個任務。sql

1.1.2   架構

 

1.1.3   特性

1)no agents:被管控主機上不需安裝任何客戶端;shell

2)no server:無服務器端,使用時直接運行命令便可;apache

3)modules in any languages:基於模塊工做,可以使用任意語言開發模塊;bash

4)yaml:使用yml定製劇本playbook;服務器

5)ssh by default:基於SSH工做;

6)strong multi-tier solution:可實現多機指揮。

1.1.4   優勢

1)輕量級,客戶端不需agent;

2)批量執行任務可寫成腳本,不用分發到遠程就可執行;

3)使用python編寫,維護更簡單;

4)支持sudo。

1.2    安裝

依賴於epel源,可經過阿里雲鏡像站配置,使用前基於ssh無密認證。

# yum -y install ansible

配置文件:/etc/ansible/ansible.cfg

Invertory:/etc/ansible/hosts

# cat /etc/ansible/hosts

[webserver]

192.168.11.11

[dbserver]

192.168.11.12

1.3    結構

Inventory

Modules

AD Hoc Commands

Playbooks

         Tasks:任務

         Variables:變量

         Templates:模板

         Handlers:處理器,由某事件觸發執行的操做

         Roles:角色

1.4    幫助

# man ansible

SEE ALSO

       ansible-playbook(1), ansible-pull(1), ansible-doc(1), ansible-vault(1), ansible-galaxy(1)

# man ansible-doc

# ansible-doc -l

# ansible-doc -s MODULE_NAME

# man ansible-play

1.5    模塊

常見模塊

command                                   

#命令模塊,默認模塊(不寫時默認有),用於遠程執行命令;

# ansible all -a 'date'     

cron

# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo kazihuo" name="test cron job" state=present'

# ansible webserver -a 'crontab -l'

group

#ansible webserver -m group -a 'name=mysql gid=306 system=yes'

user

#ansible webserver -m user -a 'name=mysql uid=306 system=yes group=mysql'

copy

# ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'

# ansible all -m copy -a 'content="Hello Ansible\nHi\n" dest=/tmp/kazihuo.ansible'

template

file

# ansible all -m file -a 'owner=mysql group=mysql mode=777 path=/tmp/kazihuo.ansible'

#link

# ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'

ping

# ansible all -m ping

service

# ansible all -a 'systemctl status httpd.service'

# ansible all -m service -a 'enabled=true name=httpd state=started'

shell

# ansible all -m user -a 'name=u1 password=000'

# ansible all -m shell -a 'echo 000000|passwd --stdin u1'

script

#將本地腳本複製到遠程主機並運行

# cat /tmp/test.sh

#!/bin/bash

echo "ansible from script" > /tmp/script.ansible

# ansible all -m script -a '/tmp/test.sh'

# ansible all -m command -a 'cat /tmp/script.ansible'

yum

# ansible all -m yum -a 'name=zsh state=present'

# ansible dbserver -m yum -a 'name=httpd state=absent'

setup

#收集遠程主機的facts(每一個被管理節點在接收並運行管理命令以前,會將本身主機相關信息,如操做系統版本、IP地址等報告給遠程的ansible主機)

# ansible all -m setup

1.6    YAML

介紹

         YAML是一個可讀性高的用來表達資料序列的格式。其參考了多種語言,包括:XML、C語言、Perl以及電子郵件格式RFC2822等。

特性

         可讀性好;

         和腳本語言的交互性好;

         有一個一致的信息模型;

         易於實現;

         可基於流來處理;

         表達能力強,擴展性好;

語法

         YAML語法和其餘高階語言相似,且可簡單表達清單、散列表、標量等數據結構,其結構(Structure)經過空格來展現,序列(Sequence)裏的項用「-」來表示,Map裏的鍵值對用「:」分隔。

 

1.7    Inventory

         用於將批量操做主機分組命名,默認的inventory file是/etc/ansible/hosts;其能夠有多個,也可經過Dynamic Inventory動態生成。

         inventory文件遵循INI文件風格,中括號中字符爲組名,同一個主機可同時歸併到多個不一樣的組中;

         當目標主機使用了非默認SSH端口,可在主機名稱後使用冒號+端口號來標明。

         eg:

         [webserver]

         192.168.10.11

         192.168.10.12:222

         當主機名遵循必定的命名模式,可用列表方式標識各個主機;

         eg:

         [webserver]

         www[01:30].kazihuo.command

         [dbserver]

         db[a:e].kazihuo.com

主機變量

         在inventory中定義主機時爲其添加主機變量以便於在playbook中使用。

         eg:

         [webservers]

         192.168.10.11 http_port=80 maxRequestsPerChild=808

組變量

         指賦予給指定組內全部主機上的在playbook中的可用變量。

         eg:

         [webservers]

         192.168.10.11

         [webservers:vars]

         ntp_server=ntp.kazihuo.com

組嵌套

         inventoy中,組能夠包含其餘的組,也可向組中的主機指定變量,這些變量只能在ansible-playbook中使用,而ansible不支持。

         eg:

         [apache]

         192.168.10.11

         [nginx]

         192.168.10.12

         [webservers:children]

         apache

         nginx

         [webservers:vars]

         ntp_server=ntp.kazihuo.com      

inventory參數

         基於ssh鏈接inventory中指定的遠程主機時,可經過參數指定其交互方式;

         ansible_ssh_host

         ansible_ssh_port

         ansible_ssh_user

         ansible_ssh_pass

         ansible_sudo_pass

         ansible_connection

         ansible_ssh_private_key_file

         ansible_shell_type

         ansible_python_interpreter

         ansible\_\*\_interpreter

1.8    playbook

         playbook是由一個或多個「play」組成的列表。play的主要功能是將事先歸併爲一組的主機裝扮成事先經過ansible中的task定義好的角色。從根本上來說,所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中,便可以讓它們聯同起來按事先編排的機制同唱一臺大戲。

1.8.1   實例說明

1.8.1.1 建立用戶

指定建立用戶與組、傳輸文件;

# cat nginx.yml

 1 - hosts: webserver
 2 
 3   remote_user: root
 4 
 5   tasks:
 6 
 7   - name: create nginx group
 8 
 9     group: name=nginx system=yes gid=208
10 
11   - name: create nginx user
12 
13     user: name=nginx uid=208 group=nginx system=yes
14 
15  
16 
17 - hosts: dbserver
18 
19   remote_user: root
20 
21   tasks:
22 
23   - name: copy file to dbserver
24 
25     copy: src=/etc/passwd dest=/tmp/passwd.ansible
nginx.yml

# ansible-playbook nginx.yml

  

1.8.1.2 處理器

指定主機經過yum部署httpd,設置開機自啓,並指定配置文件,當配置文件發生改變後,再次執行,將重啓httpd;

[root@k2 ~]# cat apache.yml

 1 - hosts: webserver
 2 
 3   remote_user: root
 4 
 5   tasks:
 6 
 7   - name: install httpd package
 8 
 9     yum: name=httpd state=present
10 
11   - name: install configuration file for httpd
12 
13     copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
14 
15     notify:
16 
17     - restart httpd   
18 
19   - name: start httpd service
20 
21     service: enabled=true name=httpd state=started
22 
23   handlers:
24 
25   - name: restart httpd
26 
27     service: name=httpd state=restarted
apache.yml

[root@k2 ~]# ansible-playbook apache.yml

 

1.8.1.3 基礎變量

基礎變量應用;

[root@k2 ~]# cat /etc/ansible/hosts

1 [webserver]
2 
3 192.168.11.11 testvar="q1"
4 
5 192.168.11.13 testvar="q3"
6 
7 [dbserver]
8 
9 192.168.11.12

[root@k2 ~]# cat test.yml

1 - hosts: webserver
2 
3   remote_user: root
4 
5   tasks:
6 
7   - name: copy file
8 
9     copy: content="{{ ansible_all_ipv4_addresses }},{{ testvar }}\n"  dest=/tmp/vars.ans
test.yml

[root@k2 ~]# ansible-playbook test.yml

 

[root@Q1 /tmp]# cat vars.ans

[u'192.168.11.11', u'192.168.10.11'],q1

[root@Q3 /tmp]# cat vars.ans

[u'192.168.11.13'],q3

1.8.1.4 進階變量

經過變量實現對不一樣主機提供不一樣的配置文件;

[root@k2 ~]# cat /etc/ansible/hosts

1 [webserver]
2 
3 192.168.11.11 http_port=80
4 
5 192.168.11.13 http_port=90
6 
7 [root@k2 ~]# cat template/httpd.conf.jinja2 |grep Listen
8 
9 Listen {{ http_port }}
hosts

[root@k2 ~]# cat apache.yml

 1 - hosts: webserver
 2 
 3   remote_user: root
 4 
 5   vars:
 6 
 7   - package: httpd
 8 
 9   - service: httpd
10 
11   tasks:
12 
13   - name: install httpd package
14 
15     yum: name={{ package }} state=present
16 
17   - name: install configuration file for httpd
18 
19     template: src=/root/template/httpd.conf.jinja2 dest=/etc/httpd/conf/httpd.conf
20 
21     notify:
22 
23     - restart httpd   
24 
25   - name: start httpd service
26 
27     service: enabled=true name={{ service }} state=started
28 
29   handlers:
30 
31   - name: restart httpd
32 
33     service: name=httpd state=restarted
apache.yml

[root@k2 ~]# ansible-playbook apache.yml

[root@Q1 ~]# netstat -atunpl |grep 80

 

[root@Q3 ~]# netstat -atunpl |grep 90

 

1.8.1.5 條件判斷

條件判斷,當知足某條件的主機才執行操做;

[root@k2 ~]# cat cond.yml

 1 - hosts: all
 2 
 3   remote_user: root
 4 
 5   vars:
 6 
 7   - username: user10
 8 
 9   tasks:
10 
11   - name: create {{ username }} user
12 
13     user: name={{ username }}
14 
15     when: ansible_fqdn == "node1,kaizhuo.com"
cond.yml

1.8.1.6 迭代機制

迭代,當有重複性執行的任務時,可以使用迭代機制;

- name: add several users

  user: name={{ item }} state=present groups=wheel

  with_items:

     - testuser1

     - testuser2

上面語句的功能等同於下面的語句:

- name: add user testuser1

  user: name=testuser1 state=present groups=wheel

- name: add user testuser2

  user: name=testuser2 state=present groups=wheel

事實上,with_items中可使用元素還可爲hashes,例如:

- name: add several users

  user: name={{ item.name }} state=present groups={{ item.groups }}

  with_items:

    - { name: 'testuser1', groups: 'wheel' }

- { name: 'testuser2', groups: 'root' }

1.8.2   roles

1.8.2.1 基礎說明

roles可以根據層次型結構自動裝載vars、tasks、handlers等,經過分別將變量、文件、任務、模板及處理器放置與單獨的目錄中,並可便捷的include全部的一種機制。

roles目錄下包含:

tasks目錄:至少包含一個main.yml文件,定義角色的任務列表;此文件可以使用include包含其餘的位於此目錄中的task文件;

files目錄:存放由copy或script等模塊調用的文件;

template目錄:template模塊自動在此目錄中尋找jinja2模板文件;

handlers目錄:此目錄中因包含一個main.yml文件,定義各handlers;

vars目錄:包含一個main.yml文件,用於定義此角色中用到的變量;

meta目錄:包含一個main.yml文件,用於定義特殊設定及其依賴關係;

default目錄:包含一個main.yml,爲當前角色設定默認變量時使用此目錄;

1.8.2.2 實例說明

[root@k2 ~]# mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}

[root@k2 ~/ansible_playbooks]# tree

 1 .
 2 
 3 ├── roles
 4 
 5 │   ├── dbsrvs
 6 
 7 │   │   ├── files
 8 
 9 │   │   │   └── my.cnf
10 
11 │   │   ├── handlers
12 
13 │   │   │   └── main.yml
14 
15 │   │   ├── meta
16 
17 │   │   ├── tasks
18 
19 │   │   │   └── main.yml
20 
21 │   │   ├── templates
22 
23 │   │   └── vars
24 
25 │   └── websrvs
26 
27 │       ├── files
28 
29 │       │   └── httpd.conf
30 
31 │       ├── handlers
32 
33 │       │   └── main.yml
34 
35 │       ├── meta
36 
37 │       ├── tasks
38 
39 │       │   └── main.yml
40 
41 │       ├── templates
42 
43 │       └── vars
44 
45 └── site.yml
46 
47 15 directories, 8 files
tree

[root@k2 ~/ansible_playbooks]# cat site.yml

 1 - hosts: 192.168.11.11
 2 
 3   remote_user: root
 4 
 5   roles:
 6 
 7   - websrvs
 8 
 9 - hosts: 192.168.11.12
10 
11   remote_user: root
12 
13   roles:
14 
15   - dbsrvs
16 
17 - hosts: 192.168.11.13
18 
19   remote_user: root
20 
21   roles:
22 
23   - websrvs
24 
25   - dbsrvs
site.yml

[root@k2 ~/ansible_playbooks]# cat roles/dbsrvs/handlers/main.yml

 1 - name: restart mariadb
 2 
 3   service: name=mariadb state=restarted
 4 
 5 [root@k2 ~/ansible_playbooks]# cat roles/dbsrvs/tasks/main.yml
 6 
 7 - name: install mariadb package
 8 
 9   yum: name={{ item }} state=present
10 
11   with_items:
12 
13   - mariadb
14 
15   - mariadb-devel
16 
17   - mariadb-server
18 
19 - name: install configuration file
20 
21   copy: src=my.cnf dest=/etc/my.cnf
22 
23   tags:
24 
25   - myconf
26 
27   notify:
28 
29   - restart mariadb
30 
31 - name: start mariadb service
32 
33   service: name=mariadb enabled=true state=started
34 
35 [root@k2 ~/ansible_playbooks]# cat roles/websrvs/handlers/main.yml
36 
37 - name: restart httpd
38 
39   service: name=httpd state=restarted
40 
41 [root@k2 ~/ansible_playbooks]# cat roles/websrvs/tasks/main.yml
42 
43 - name: install httpd package
44 
45   yum: name=httpd
46 
47 - name: install configuration file
48 
49   copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
50 
51   tags:
52 
53   - conf
54 
55   notify:
56 
57   - restart httpd
58 
59 - name: start httpd
60 
61   service: name=httpd state=started
main.yml

[root@k2 ~/ansible_playbooks]# ansible-playbook site.yml

 

相關文章
相關標籤/搜索