Ansible簡介
Ansible是2013年推出的一款IT自動化和DevOps軟件,目前由Redhat已簽署Ansible收購協議。其是基于Python研發(fā),糅合了人多老運維工具的優(yōu)點實現了批量操作系統配置,批量程序的部署显设,批量運行命令等功能。
Ansible的特性:
-
模塊化設計
- 調用特定的模塊來完成特定任務
-
基于pythone語言實現
- 由paramiko,PyYAML(半結構化語言)和jinja2三個關鍵模塊實現
-
部署簡單
- 是agent less類型
-
主從模式工作
-
支持自定義模塊
-
支持playbook
-
易于使用
-
支持多層部署
-
支持異構IT環(huán)境
Ansible的組件
-
ansible core
- 核心模塊
-
host inventory
- 主機庫辛辨,能夠管控的主機列表捕捂,沒有放進這個列表的,都不能進行管控
-
connection plugins
- 連接插件斗搞,一般默認基于ssh協議連接
-
modules
- core modules(自帶模塊)
- custom modules(自定義模塊)
-
plugins
- 為ansible擴展功能
-
playbook
- 劇本绞蹦,按照所設定編排的順序執(zhí)行完成安排的任務
ansible的配置文件
-
主配置文件
- /etc/ansible/ansible.cfg
-
host inventory定義管控主機
- /etc/ansible/hosts (遵循INI風格,中括號中的字符是組名榜旦,一個主機 可以屬于多個組)
[webservers] node1.zhenping.me node2.zhenping.me 172.16.36.70 172.16.36.71
ansible安裝
- yum install ansible
ansible命令
-
使用格式
-
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-
-a
:指定模塊的參數 -
-f #
:一次要管理幾個主機幽七,默認為5個 -
-m MOUDULE
: 指定模塊 -
<host-pattern>
: 事先需要定義的hosts文件中的主機列表 -
-i PATH
: 指明使用的host inventory文件路徑
-
-
ansible-doc命令
獲取模塊列表,及模塊使用格式
-
使用格式
-
ansible-doc [-l] [-m MODULE]
-
-l
: 列出支持的核心模塊 -
-s MODULE
: 查看模塊的用法
-
-
ansible常用模塊
一溅呢、command模塊
-
功能
- 能在遠程節(jié)點運行一個命令澡屡,但不能運行有管理的命令,command模塊的參數非為kv格式,而是直接給出要執(zhí)行的命令
-
使用示例
-
ansible all -a 'ntpdate 172.16.0.1'
(其是默認模塊咐旧,可省略模塊名稱)
-
[root@Centos7 ~]# ansible all -a 'ntpdate 172.16.0.1'
172.16.36.61 | success | rc=0 >>
29 Feb 19:36:07 ntpdate[2360]: step time server 172.16.0.1 offset 135.642843 sec
172.16.36.60 | success | rc=0 >>
29 Feb 19:36:07 ntpdate[2402]: step time server 172.16.0.1 offset 135.687205 sec
172.16.36.71 | success | rc=0 >>
29 Feb 19:36:13 ntpdate[2345]: step time server 172.16.0.1 offset -28665.158303 sec
172.16.36.70 | success | rc=0 >>
29 Feb 19:36:13 ntpdate[32087]: step time server 172.16.0.1 offset -28664.450998 sec
二驶鹉、user模塊
-
功能
管理用戶
-
使用格式
-
ansible all -m user -a "name= state={present | absent} force= system= uid= shell= home= "
- name= : 創(chuàng)建的用戶名
- state= : present新增,absent刪除
- force= : 刪除用戶的時候刪除家目錄
- system= : 創(chuàng)建系統用戶
- uid= : 指定UID
- shell= : 指定shell
- home= : 指定用戶家目錄
-
-
使用示例
ansible webserver -m user -a "name=zhenping state=present system=true"
ansible webserver -m user -a "name=zhenping state=absent"
三铣墨、group模塊
-
功能
組管理
-
使用格式
-a "name= state={present|absent} gid= system="
-
使用示例
ansible webserver -m group -a "name=user1 state=present system=true"
四室埋、cron模塊
-
功能
定義cron任務
-
使用格式
-a "name= state= minute= hour= day= month= weekday= job= state={present|absent"
-
使用示例
ansible all -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.16.0.1 &> /dev/null' name='sync time' state=present"
五、ping模塊
-
功能
測試網絡連通性, ping模塊沒有參數
-
使用格式
ansible all -m ping
-
使用示例
[root@Centos7 ~]# ansible all -m ping
172.16.36.61 | success >> {
"changed": false,
"ping": "pong"
}
172.16.36.60 | success >> {
"changed": false,
"ping": "pong"
}
172.16.36.70 | success >> {
"changed": false,
"ping": "pong"
}
172.16.36.71 | success >> {
六伊约、file模塊
功能
文件創(chuàng)建和刪除
使用格式
-
-a "path= mode= owner= group= state={directory|link|hard|touch|file|absent} src= "
- path= : 目標
- src= : 原文件
- state= : 文件類型
使用示例
ansible webserver -m file -a "path=/root/fstab src=/etc/fstab state=link"
~]# ansible webserver -m file -a "path=/root/testfile state=directory"
七姚淆、copy模塊
功能
文件復制,把管理端的文件復制到遠程主機各一份
使用格式
-a "src= dest= mode= owner= "
使用示例
ansible webserver -m copy -a "src=/root/testfile.txt dest=/root/testfile.ansible"
八屡律、yum模塊
功能
實現程序包安裝及管理
使用格式
-
-a "name= conf_file= state={present|latest|absent} enablerepo= disablerepo= "
- state=latest : 安裝最新版本
- conf_file= : 使用指定repo的配置文件
使用示例
ansible webserver -m yum -a 'name=nginx state=absent'
九腌逢、service模塊
功能
實現服務管理
使用格式
-a "name= state={started|stoped|restarted} enabled= runlevel= "
使用示例
ansible webserver -m service -a "name=nginx state=started enabled=true
十、shell模塊
功能
運行shell命令超埋,其是啟動一個子shell進程來運行命令搏讶,它可以支持管道傳送
使用格式
-a 'command'
使用示例
ansible webserver -m shell -a "echo "zhenping.me" | passwd --stdin user1"
十一、script模塊
功能
指定本地的腳本文件霍殴,到遠程主機運行一次
使用格式
-a "/path/to/script"
使用示例
*ansible webserver -m script -a '/root/test.sh'
十二媒惕、setup模塊
功能
收集遠程指定主機的facts信息,其將收集的信息保存在各變量中来庭,變量引用方法為:直接引用名稱
使用示例
ansible 172.16.36.71 -m setup
[root@Centos7 ~]# ansible 172.16.36.71 -m setup
172.16.36.71 | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.36.71"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fed1:dcb5"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/20/2014",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-229.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rhgb": true,
"ro": true,
"root": "UUID=cdd3548e-caf3-46cb-b81c-03f443325edc"
},
"ansible_date_time": {
"date": "2016-02-29",
"day": "29",
"epoch": "1456751201",
"hour": "21",
"iso8601": "2016-02-29T13:06:41Z",
"iso8601_micro": "2016-02-29T13:06:41.194636Z",
"minute": "06",
"month": "02",
"second": "41",
"time": "21:06:41",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Monday",
"year": "2016"
},
"ansible_default_ipv4": {
"address": "172.16.36.71",
"alias": "eno16777736",
"gateway": "172.16.0.1",
"interface": "eno16777736",
"macaddress": "00:0c:29:d1:dc:b5",
"mtu": 1500,
"netmask": "255.255.0.0",
"network": "172.16.0.0",
"type": "ether"
},
.........
playbooks劇本
簡介
playbooks是ansible更為強大的配置管理組件妒蔚,實現基于文本文件編排執(zhí)行的多個任務,且多次重復執(zhí)行。其是使用YAML(Yet Another Markup Language),類似于半結構化語言面睛,聲明式配置絮蒿,可讀性較高尊搬。易于與腳本語言交互
核心組件
-
Tasks
:任務 -
Variables
: 變量-
inventory參數:(用于hosts定義的主機后面叁鉴,多個使用空格分隔)
- ansible_ssh_port : 指定ssh端口
- ansible_ssh_user : 指定ssh用戶
- ansible_ssh_pass : 指定ssh用戶登錄認證密碼,明文密碼
- ansible_sudo_pass : 指明sudo時候的密碼
[webserver] 172.16.36.70 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=zhenping.me
-
-
template
: 模板 -
Handles
: 處理器 -
Roles
: 角色佛寿,用于調度不同的playbooks
YAML語法格格式
任何數據結構都用縮進進來標識幌墓,可以嵌套
每一行是一個鍵值數據key:value,使用冒號分隔冀泻,若想在一行標識需要使用{}和,將多個kv分隔開
列表使用
-
標識示例:
- hosts: webserver
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: start nginx
service: name=nginx state=started enabled=true
- name: install php-fpm
yum: name=php-fpm state=present
- name: start php-fpm
service: name=php-fpm state=started enabled=true
vars
handlers
- hosts: dbserver
remote_user: root
tasks:
- name: install mysql
yum: name=mysql state=present
playbook的變量
變量命名方式
字母常侣、數字和下劃線組成,僅能以字母開頭
變量的各類
-
facts
- 由遠程主機發(fā)回的主機特有的屬性信息弹渔,這些信息被保存在ansible變量中胳施,無須聲明,可直接調用
-
自定義變量
- 通過命令行傳遞
- ~]# ansible-playbook test.yml -extra-vars "hosts=www user=zhenping"
- 通過roles傳遞
- 通過命令行傳遞
-
主機變量
- 定義在inventory中的主機之后的變量肢专,直接傳遞給單個主機的變量
[webserver] 172.16.36.70 username=zhenping pass=zhenping 172.16.36.71
-
主機組變量
- 定義在inventory中的組上的變量
[webserver] 172.16.36.70 172.16.36.60 [webserver:vars] var1=value var2=value
playbook調用變量的示例
- hosts: webserver
remote_user: root
vars:
user: user2
pass: 123321
tasks:
- name: add user
user: name={{ user }} state=present
- name: set password
shell: /bin/echo {{ pass }} | passwd --stdin {{ user }}
手動運行playbook腳本時舞肆,傳遞變量(手動傳遞時,優(yōu)先級要高于腳本中的變量)
ansible-playbook script.yml --extra-vars "username=ubunt"
ansible-playbook script.yml -e VARS
playbook的條件測試
在某task后面添加when子句博杖,即可實現條件測試功能椿胯,when語句支持jinja2語法
####當主機的操作系統為redhat系列操作系統時,才執(zhí)行yum安裝
- hosts : webserver
remote_user : root
tasks :
- name: yum install keepalived
yum: name=keepalived state=present
when: ansible_os_family == "ReaHat"
- name: say hello
shell: /bin/echo "hello world"
playbook的迭代
在task中調用內置的Item變量剃根,在某task后面使用with_items語句來定義元素列表
- hosts: webserver
remote_user: root
tasks:
- name: user add
user: name={{ item }} state=present
with_items:
- testuser1
- testuser2
- testuser3
- testuser4
- hosts: webserver
remote_user: root
tasks:
- name: user add
user: name={{ item.name }} state=present group={{ item.groups }}
with_items:
- { name: 'tom2', groups: 'tom' }
- { name: 'tom2', groups: 'tom2' }
playbook--handlers: 處理器哩盲、觸發(fā)器
只有在其關注的條件滿足時,才會被觸發(fā)執(zhí)行的任務
示例:只有原配置文件被修改了狈醉,才會重新加載服務
- hosts: webserver
remote_user: root
tasks:
- name: yum install nginx
yum: name=nginx state=present
- name: start nginx
service: name=nginx state=started enabled=true
- name: copy configuration file
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
notify: restart nginx
handlers:
- name: restart nginx
service: name=nginx state=reloaded
Roles: 角色
roles用于實現“代碼復用”廉油,讓playboo中的各元素組織起來,roles以特定的層次型格式組織起來的playbook元素(vars,tasks,template,handlers),可被playbook以roles的名字直接調用
roles目錄結構
-
webserver/
: 主機組文件夾-
files/
: 存放文件苗傅,此角色中用到的所有文件均放置于此目錄中 -
templates/
: jinja2模板文件存放位置 -
tasks/
: 任務列表文件娱两,可以有多個,但至少有一個叫做main.yml的文件金吗,其它文件要被main.yml包含 -
handlers/
: 處理器列表文件十兢,可以有多個,至少有一個叫做main.yml的文件摇庙,其它文件要被main.yml包含 -
vars/
: 變量字典文件旱物,可以有多個, 但至少有一個叫做main.yml的文件卫袒,其它文件要被main.yml包含 -
meta/
: 元數據宵呛,用于定義此角色的特殊設定及依賴關系等
-
示例:
~]# cd /etc/ansible/soles
~]# mkdir -pv webserver/{files,vars,tasks,handlers,meta,templates}
~]# cd webserver/
~]# vim vars/main.yml
user: daemon
group: daemon
~]# vim tasks/main.yml
- name: remove nginx
yum: name=nginx state=absent
- name: install apache
yum: name=httpd state=present
- name: start & enable httpd
service: name=httpd state=started enabled=true
- name: install configuration file
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd servic
~]# vim handlers/main.yml
- name: restart httpd service
service: name=httpd state=restarted
~]# cp /etc/httpd/conf/httpd.conf /root/webserver/template/httpd.conf.j2 #準備示例文檔
~]# vim templates/http.conf.j2
....
User {{ user }}
Group {{ group }}
....
~]# cd ../.. #與roles同級的目錄
~]# vim webserver.yml
- hosts: webserver
remote_user: root
roles:
- webserver
~]# ansible-playbook webserver.yml
PLAY [webserver] **************************************************************
GATHERING FACTS ***************************************************************
ok: [172.16.36.70]
ok: [172.16.36.71]
TASK: [webserver | remove nginx] **********************************************
ok: [172.16.36.70]
ok: [172.16.36.71]
TASK: [webserver | install apache] ********************************************
ok: [172.16.36.70]
ok: [172.16.36.71]
TASK: [webserver | start & enable httpd] **************************************
ok: [172.16.36.71]
ok: [172.16.36.70]
TASK: [webserver | install configuration file] ********************************
ok: [172.16.36.70]
ok: [172.16.36.71]
PLAY RECAP ********************************************************************
172.16.36.70 : ok=5 changed=0 unreachable=0 failed=0
172.16.36.71 : ok=5 changed=0 unreachable=0 failed=0
#####可以為roles傳遞變量
~]# vim dbserver.yml
- hosts: 172.16.36.60
remote_user: root
roles:
- { role: webserver, user=nobody }
- { role: dbserver, user=mysql } #可以調用多個role
######使用tags參數,只調用某個tasks(以下示例只觸發(fā)了install config的任務)
~]# vim roles/webserver/tasks/main.yml
- name: remove nginx
yum: name=nginx state=absent
- name: install apache
yum: name=httpd state=present
- name: start & enable httpd
service: name=httpd state=started enabled=true
- name: install configuration file
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd service
tags: conf
[root@Centos7 ~]# ansible-playbook -t conf webserver.yml
PLAY [webserver] **************************************************************
GATHERING FACTS ***************************************************************
ok: [172.16.36.71]
ok: [172.16.36.70]
TASK: [webserver | install configuration file] ********************************
ok: [172.16.36.70]
ok: [172.16.36.71]
PLAY RECAP ********************************************************************
172.16.36.70 : ok=2 changed=0 unreachable=0 failed=0
172.16.36.71 : ok=2 changed=0 unreachable=0 failed=0