Ansible系列命令有以下:php
ansible
:這個命令是平常工做中使用率很是高的命令之一,主要用於臨時一次性操做;ansible-doc
:是Ansible模塊文檔說明,針對每一個模塊都有詳細的用法說明和應用案例介紹;ansible-galaxy
:能夠簡單的理解爲Github或PIP的功能,經過ansible-galaxy,咱們能夠下載安裝優秀個Roles;ansible-playbook
:是平常應用中使用頻率最高的命令,其工做機制是,經過讀取預先編寫好的playbook文件實現批量管理;ansible-pull
:Ansible的另外一種工做模式,pull模式,ansible默認使用push模式;ansible-vault
:主要用於配置文件加密;ansible-console
:讓用戶能夠在ansible-console虛擬出來的終端上像Shell同樣使用Ansible內置的各類命令;上一篇已經介紹過ansible
和ansible-doc
的基本用法,下面再來介紹一下其餘系列命令的用法。html
用法:ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...node
去 https://galaxy.ansible.com/ 上傳或下載優秀的Playbook(roles)python
#列出全部已安裝的galaxy ansible-galaxy list #安裝galaxy ansible-galaxy install geerlingguy.redis #刪除galaxy ansible-galaxy remove geerlingguy.redis
用法:ansible-vault [create|decrypt|edit|encrypt|rekey|view] [--help] [options] vaultfile.ymlmysql
ansible-vault encrypt hello.yml # 加密 ansible-vault decrypt hello.yml # 解密 ansible-vault view hello.yml # 查看
root@mageduweb (2)[f:10] $ service name=httpd state=stopped root@mageduweb (2)[f:10] $ 執行用戶@當前操做的主機組 (當前組的主機數量)[f:併發數]$ 設置併發數: forks n 例如: forks 10 切換組: cd groupname 例如: cd mageduweb 列出當前組主機列表: list 列出全部的內置模塊: ?
Usage: ansible-playbook playbook.ymlnginx
playbook就是一個用yaml
語法把多個模塊堆起來的一個文件而已。git
1.簡介
yaml是一個可讀性高的用來表達資料序列的格式。YAML參考了其餘多種語言,包括:XML、C語言、Python、Perl以及電子郵件格式RFC2822等。Clark Evans在2001年在首次發表了這種語言,另外Ingy döt Net與Oren Ben-Kiki也是這語言的共同設計者。github
2.特色web
YAML的語法和其餘高階語言相似,而且能夠簡單表達清單、散列表、標量等數據結構。其結構(Structure)經過空格來展現,序列(Sequence)裏的項用"-"來表明,Map裏的鍵值對用":"分隔。下面是一個示例。redis
- hosts: 10.1.0.1 #定義主機 vars: #定義變量 var1: value var2: value tasks: #定義任務 - name: #任務名稱。 #這裏就能夠開始用模塊來執行具體的任務了。 handlers: #定義觸發通知所做的操做。裏面也是跟tasks同樣,用模塊定義任務。 - name: remote_user: #遠程主機執行任務時的用戶。通常都是root,通常也不用指定。 - hosts: web vars: tasks: handlers: remote_user:
YAML文件擴展名一般爲.yaml,如example.yaml
Playbooks
:
核心組件:
Hosts
:執行的遠程主機列表Tasks
:任務,由模塊定義的操做的列表;Varniables
:內置變量或自定義變量在playbook中調用Templates
:模板,即便用了模板語法的文本文件;Handlers
:和nogity結合使用,爲條件觸發操做,知足條件方纔執行,不然不執行;Roles
:角色;* 安裝http腳本實現: #!/bin/bash # 安裝Apache yum install --quiet -y httpd httpd-devel # command: yum install --quiet -y httpd httpd-devel # 複製配置文件 cp /path/to/config/httpd.conf /etc/httpd/conf/httpd.conf -f cp/path/to/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf -f # 啓動Apache,並設置開機啓動 service httpd start chkconfig httpd on * 安裝httpd ansible-playbook實現 --- - hosts: all tasks: - name: "安裝Apache" command: yum install --quiet -y httpd httpd-devel - name: "複製配置文件" command: cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf command: cp /tmp/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf - name: "啓動Apache,並設置開機啓動" command: service httpd start command: chkconfig httpd on
playbook的基礎組件:
Hosts:運行指定任務的目標主機; remote_user:在遠程主機以哪一個用戶身份執行; sudo_user:非管理員須要擁有sudo權限; tasks:任務列表 模塊,模塊參數: 格式: (1) action: module arguments (2) module: arguments
示例:
vim test.yaml,後綴也能夠是.yml
- hosts: all remote_user: root tasks: - name: install a group group: name=mygrp system=true - name: install a user user: name=user1 group=mygrp system=true - hosts: websrvs remote_user: root tasks: - name: install httpd package yum: name=httpd - name: start httpd service service: name=httpd state=started
運行playbook,使用ansible-playbook命令:
檢測語法:
測試運行,即不真正執行:
--list-hosts --list-tasks --list-tags
運行:
-t TAGS, --tags=TAGS --skip-tags=SKIP_TAGS 跳過指定的標籤 --start-at-task=START_AT 從哪一個任務後執行
tags
: 標籤,給指定的任務定義一個調用標識;
- name: NAME module: arguments tags: TAG_ID
指定某條任務執行:ansible-playbook --tags=user useradd.yml
- hosts: mageduweb remote_user: root tasks: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx user: name=nginx state=present group=nginx - name: Install Nginx yum: name=nginx state=present - name: Start Nginx service: name=nginx state=started enabled=yes
能夠一次調用多個名稱相同的標籤。也能夠調用不一樣的標籤用 「,」 分割。
示例:
咱們把httpd的監聽端口改成8080
用playbook把這個文件傳到node3,4上
指明標籤的檢查
從標籤位置開始執行
跳過標籤
handlers 和 notify 結合使用觸發條件,讓playbook在知足必定觸發條件時纔去執行某條task
- name: TASK_NAME module: arguments notify: HANDLER_NAME handlers: - name: HANDLER_NAME module: arguments
第一次的話都會運行,後邊若是文件內容發生改變就會觸發notify,而後會直接執行handlers的內容(這裏notify後邊的事件就都不會執行了)。估計是md5那種的校驗。刪了個#號居然也會通知。
--- - hosts: mageduweb remote_user: root tasks: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx user: name=nginx state=present group=nginx - name: Install Nginx yum: name=nginx state=present - name: config copy: src=/root/config.txt dest=/etc/nginx/config.txt notify: - Restart Nginx - Check Nginx Process handlers: - name: Restart Nginx service: name=nginx state=restarted enabled=yes - name: Check Nginx Process shell: ss -tnl | grep 80
變量來源:
(1)ansible setup facts遠程主機的全部變量均可以用
(2)自定義變量
a. 在/etc/ansible/hosts 定義變量,在主機組中的主機單獨定義,優先級高於組中公共變量
b. 在/etc/ansible/hosts 定義變量,針對主機組中的全部主機集中定義變量
c. 經過命令行指定變量,優先級最高
在hosts Inventory(/etc/ansible/hosts)中爲每一個主機定義專用變量值; [all] 172.16.47.103 aaa=nginx 172.16.47.104 aaa=httpd ############################### - hosts: all remote_user: root tasks: - name: install web package yum: name={{ aaa }} state=latest
(a) 向不一樣的主機傳遞不一樣的變量; IP/HOSTNAME variable_name=value [web] 10.1.6.72 qzx=httpd 10.1.6.73 qzx=nginx (b) 向組內的全部主機傳遞相同的變量 ; [groupname:vars] variable_name=value [web:qzx] qzx=httpd [web] 10.1.6.72 10.1.6.73
變量調用方式:
{{ variable_name }} 經過{{ }} 調用變量,且變量和{}兩頭之間必須有空格
額外介紹的是:
在playbook中調用變量時,有時"{{ variable_name }}"須要要雙引號引發來方可生效,有時候必須不能用""引發來
d.在playbook中定義,建議使用這個!
vars: - var_name: value - var_name: value
e.Inventory還可使用參數
用於定義ansible遠程鏈接目標主機時使用的屬性,而非傳遞給playbook的變量; ansible_ssh_host ansible_ssh_port ansible_ssh_user ansible_ssh_pass ansible_sudo_pass ...
f.在角色調用時傳遞
roles: - { role: ROLE_NAME, var: value, ...}
文本文件,內部嵌套有模板語言腳本(使用模板語言編寫)
Jinja2 是由python編寫的。 在咱們打算使用基於文本的模板語言時,jinja2是很好的解決方案。yeml
是寫playbook
,jinja2
是寫配置文件模板
的。
功能:
將模板的文件的變量值轉換成對應的本地主機的肯定值。例如:ansible端寫一個內建變量{{ ansible_processor_vcpus }}
,當這個文件被複制到對應主機時會自動生成對應主機 cpu的顆數的結果替換之。
Jinja2語法:
字面量: 字符串:使用單引號或雙引號; 數字:整數、浮點數; 列表:[item1, item2, ...] 元組:(item1, item2, ...) 字典:{key1:value1, key2:value2, ...} 布爾型:true/false 算術運算: +, -, *, /, //, %, ** 比較操做: ==, !=, >, <, >=, <= 邏輯運算:and, or, not
執行模板文件中的腳本,並生成結果數據流,須要使用template模塊
;
template
:使用了Jinjia2格式做爲文件模版,進行文檔內變量的替換的模塊。至關於copy。
將jinja2的文件模板理解並執行,轉化爲各個主機間的對應值
backup 創建個包括timestamp在內的文件備份,以備不時之需. dest 遠程節點上的絕對路徑,用於放置template文件。 group 設置遠程節點上的的template文件的所屬用戶組 mode 設置遠程節點上的template文件權限。相似Linux中chmod的用法 owner 設置遠程節點上的template文件所屬用戶 src 本地Jinjia2模版的template文件位置。
注意:此模板不能在命令行使用,而只能用於playbook;用法同copy
(1) templates文件必須存放於目錄名爲templates下,且命名爲 .j2 結尾 (2)yaml/yml playbook文件需和templates目錄平級,目錄結構以下: ./ ├── temnginx.yml └── templates └── nginx.conf.j2
案例1:
// templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server { listen {{ vhost }} } {% endfor %} // temnginx.yml --- - hosts: mageduweb remote_user: root vars: nginx_vhosts: - web1 - web2 - web3 # nginx_vhosts: # - listen: 8080 tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
生成的結果:
server { listen web1 } server { listen web2 } server { listen web3 }
案例2: shell腳本與Jinja語法對比: -- shell腳本中寫法 for i in {1..10} do con done if [ xx ];then con elif con else con fi -- Jinja寫法: // templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server { listen {{ vhost.listen }} } {% endfor %} * 生成的結果 server { listen 8080 } * playbook調用文件 // temnginx.yml --- - hosts: mageduweb remote_user: root vars: nginx_vhosts: - listen: 8080 tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf * 案例3 // temnginx.yml --- - hosts: mageduweb remote_user: root vars: nginx_vhosts: - web1: listen: 8080 server_name: "web1.magedu.com" root: "/var/www/nginx/web1/" - web2: listen: 8080 server_name: "web2.magedu.com" root: "/var/www/nginx/web2/" - web3: listen: 8080 server_name: "web3.magedu.com" root: "/var/www/nginx/web3/" ## 案例1 # nginx_vhosts: # - web1 # - web2 # - web3 ## 案例2 tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf // templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server { listen {{ vhost.listen }} server_name {{ vhost.server_name }} root {{ vhost.root }} } {% endfor %} * 生成結果: server { listen 8080 server_name web1.magedu.com root /var/www/nginx/web1/ } server { listen 8080 server_name web2.magedu.com root /var/www/nginx/web2/ } server { listen 8080 server_name web3.magedu.com root /var/www/nginx/web3/ } * 案例4 // templates/nginx.conf.j2 {% for vhost in nginx_vhosts %} server { listen {{ vhost.listen }} {% if vhost.server_name is defined %} server_name {{ vhost.server_name }} {% endif %} root {{ vhost.root }} } {% endfor %} // temnginx.yml --- - hosts: mageduweb remote_user: root vars: nginx_vhosts: - web1: listen: 8080 #server_name: "web1.magedu.com" root: "/var/www/nginx/web1/" - web2: listen: 8080 server_name: "web2.magedu.com" root: "/var/www/nginx/web2/" - web3: listen: 8080 server_name: "web3.magedu.com" root: "/var/www/nginx/web3/" ## 案例1 # nginx_vhosts: # - web1 # - web2 # - web3 ## 案例2 tasks: - name: template config to remote hosts template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf * 執行命令 ansible-playbook temnginx.yml * 生成的結果 server { listen 8080 root /var/www/nginx/web1/ } server { listen 8080 server_name web2.magedu.com root /var/www/nginx/web2/ } server { listen 8080 server_name web3.magedu.com root /var/www/nginx/web3/ } ### ansible-playbook when條件判斷 --- - hosts: mageduweb remote_user: root tasks: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx user: name=nginx state=present group=nginx - name: Install Nginx yum: name=nginx state=present - name: restart Nginx service: name=nginx state=restarted when: ansible_distribution_major_version == "6" ### ansible-playbook with_items 列表 --- - hosts: mageduweb remote_user: root tasks: - name: create rsyncd file copy: src={{ item }} dest=/tmp/{{ item }} with_items: - a - b - c - d - name: yum install httpd yum: name={{ item }} state=present with_items: - apr - apr-util - httpd * with_itmes 嵌套子變量 --- - hosts: mageduweb remote_user: root tasks: - name: add several users user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name: 'testuser1' , groups: 'wheel'} - { name: 'testuser2' , groups: 'root'}
一、普通示例:
這裏/root/nginx.conf內容發生了改變。
- hosts: ngxsrvs remote_user: root tasks: - name: install nginx package yum: name=nginx state=latest - name: install conf file template: src=/root/nginx.conf.j2 dest=/etc/nginx/nginx.conf tags: ngxconf notify: reload nginx service - name: start nginx service service: name=nginx state=started enabled=true handlers: - name: reload nginx service shell: /usr/sbin/nginx -s reload
二、條件測試:
when語句:在tasks中使用,Jinja2的語法格式;
- hosts: all remote_user: root tasks: - name: install nginx package yum: name=nginx state=latest - name: start nginx service on CentOS6 shell: service nginx start when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" - name: start nginx service shell: systemctl start nginx.service when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
三、循環:迭代,須要重複執行的任務;
對迭代項的引用,固定變量名爲"item」,使用with_item屬性給定要迭代的元素; 這個是以任務爲中心,圍繞每一個任務來跑主機,若是中間某個任務中斷,那麼全部主機之後的任務就沒法安裝。
元素:
基於字符串列表給出元素示例:
- hosts: websrvs remote_user: root tasks: - name: install packages yum: name={{ item }} state=latest with_items: - httpd - php - php-mysql - php-mbstring - php-gd
基於字典列表給元素示例:item.name .
後邊的表示鍵
- hosts: all remote_user: root tasks: - name: create groups group: name={{ item }} state=present with_items: - groupx1 - groupx2 - groupx3 - name: create users user: name={{ item.name }} group={{ item.group }} state=present with_items: - {name: 'userx1', group: 'groupx1'} - {name: 'userx2', group: 'groupx2'} - {name: 'userx3', group: 'groupx3'}
以特定的層級目錄結構進行組織的tasks、variables、handlers、templates、files等;至關於函數的調用把各個事件切割成片斷來執行。
mkdir ./{nginx,memcached,httpd,mysql}/{file,templates,vars,handlers,meta,default,tasks} -pv
role_name/
files/:存儲由copy或script等模塊調用的文件; tasks/:此目錄中至少應該有一個名爲main.yml的文件,用於定義各task;其它的文件須要由main.yml進行「包含」調用; handlers/:此目錄中至少應該有一個名爲main.yml的文件,用於定義各handler;其它的文件須要由main.yml進行「包含」調用; vars/:此目錄中至少應該有一個名爲main.yml的文件,用於定義各variable;其它的文件須要由main.yml進行「包含」調用; templates/:存儲由template模塊調用的模板文本; meta/:此目錄中至少應該有一個名爲main.yml的文件,定義當前角色的特殊設定及其依賴關係;其它的文件須要由main.yml進行「包含」調用; default/:此目錄中至少應該有一個名爲main.yml的文件,用於設定默認變量;
在playbook中調用角色的方法:
- hosts: HOSTS remote_user: USERNAME roles: - ROLE1 - ROLE2 - { role: ROLE3, VARIABLE: VALUE, ...} - { role: ROLE4, when: CONDITION }
事例: 基於角色的方式安裝 nginx
1.建立須要的文件
mkdir ./{nginx,memcached,httpd,mysql}/{files,templates,vars,handlers,meta,default,tasks} -pv
2.複製相應的安裝包和模板到對應目錄下
3.寫tasks/下的主main.yml
- name: copy nginx package copy: src=nginx-1.10.0-1.el7.ngx.x86_64.rpm dest=/tmp/nginx-1.10.0-1.el7.ngx.x86_64.rpm - name: install nginx package yum: name=/tmp/nginx-1.10.0-1.el7.ngx.x86_64.rpm state=present - name: install nginx.conf file template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf tags: ngxconf notify: reload nginx service - name: install default.conf file template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf tags: ngxconf notify: reload nginx service - name: start nginx service service: name=nginx enabled=true state=started
4.根據須要修改nginx的配置文件模板。(這裏改的是work進程生成數和監聽的端口)
5.寫handlers目錄和vars/下的main.yml 文件
6.寫須要運行的主yml文件
7.測試
8.運行
安裝成功
9.改端口測試,經過傳遞參數方式
修改爲功!!!
http://galaxy.ansible.com
https://galaxy.ansible.com/explore#/
http://github.com/
http://ansible.com.cn/
https://github.com/ansible/ansible
https://github.com/ansible/ansible-examples
最後打個廣告,Stanley老師的Ansible權威指南,你,值得擁有