playbooks是 一個不一樣於使用Ansible命令行執行方式的模式,其功能更強大靈活。簡單來講,playbook是一個很是簡單的配置管理和多主機部署系統,不一樣於任何已經存在的模式,可做爲一個適合部署複雜應用程序的基礎。Playbook能夠定製配置,能夠按照指定的操做步驟有序執行,支持同步和異步方式。值得注意的是playbook是經過YAML格式來進行描述定義的。linux
hosts: websrvs
hosts: 192.168.16.10 remote_user: root
tasks:
name: test connection
ping:
remote_user: root
sudo: yes
sudo_user: rootnginx
3. tasks:任務列表;格式有兩種: action: module argument module: argument 建議使用 注意:shell和command模塊後面跟命令,而非key=value 某任務的狀態在運行後changed時,可經過‘notify’通知給相應的handlers 任務能夠經過‘tags’打標籤,然後可在absible-playbook命令上使用-t指定進行調用 示例: ```bash tasks: - name: disable selinux command: /sbin/setenforce 0
若是命令或者腳本的退出碼不爲零,可使用一下方式替代web
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand || /bin/true
或者shell
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand ignore_errors: True
YAML的語法和其餘高階語言相似而且能夠簡單表達清單、散列表、標量等數據結構,其結構經過空格來展現。(列表用橫杆表示,鍵值對用冒號分割,鍵值對裏又能夠嵌套另外的鍵值對)
YAML文件擴展名一般爲.yaml或者.yml。下面爲示例
必定要對齊,只能使用空格 apache
name: wkx #冒號後有空格 age: 28 gender: male spouse: name: rlt age: 27 gender: female children: - name: dabao #橫槓後有空格 age: 4 gender: femle - name: xiaobao age: 1 gender: male
ansible-playbook <filename.yaml> ... [options]
經常使用選項:
--check 只檢測可能會發生的改變,但不真正執行操做
--list-hosts 列出運行任務的主機
--limit 主機列表,只針對主機列表中的主機執行
-v 顯示過程 -vv -vvv 更詳細
ansible-playbook file.yaml --check 只檢測
Ansible-playbook file.yaml 執行
Ansible-playbook file.yaml --limit websevs 僅websevs主機執行編程
Shell腳本bash
#!/bin/bash # install apache yum -y install httpd #copy conf file cp /tmp/http.conf /etc/httpd/conf/httpd.conf #start service systemctl enable httpd systemctl start httpd
Palybook數據結構
- hosts: wensevs remote_user: root tasks: - name: install apache yum: name=httpd - name: copy conf file copy: src=/tmp/http.conf dest=/etc/httpd/conf/httpd.conf - name: start service service: name=httpd state=started enabled=yes
Handlers:是task列表,這些task與前述的task並無本質的區別,用於當關注的資源發生變化時,纔會採起必定的操做
Notify:此action被用於每一個play的最後被觸發,這樣能夠避免屢次有改變發生時每次都執行指定的操做,盡在全部變化發生後完成一次性的執行指定操做。在notify中列出的操做稱爲handler,也即notify中調用handler中定義的操做。
示例:
Handles和notify的使用:異步
- hosts: websrvs remote_user: root tasks: - name: add group nginx tags: user user: name=nginx state=present - name: add user nginx group: name=nginx state=present group=nginx - name: install nginx yum: name=nginx state=present - name: copy file copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf notify: - restart nginx - check nginx process handlers: - name: restart nginx #必須和notify中定義的名字一致 service: name=nginx status=restarted enabled=yes - check nginx process shell: killall -0 nginx > /tmp/nginx.log
Tags的使用編程語言
1. hosts: wensrvs remote_user: root tasks: 2. name: install httpd yum: name=httpd status=present 3. name: copy conf file copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf tags: conf 4. name: start service tags: service service: name=httpd state=started enabled=yes
變量名:僅能由字母、數字和下劃線組成,且只能以字母開頭
變量來源:
cat vars.yaml var1: httpd var2: nginx cat var.yaml - hosts: 192.168.16.115 remote_user: root vars_files: - vars.yaml tasks: - name: touch httpd.txt file: name={{var1}}.txt state=touch #默認建立在當前用戶下 - name: touch nginx.log file: name={{var2}}.log state=touch
使用setup變量
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: create log file file: name=/root/ansible-test/{{ ansible_fqdn }} state=touch #{{ ansible_fqdn }}爲setup模塊中域名的一個內置變量
命令行使用變量
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: create log file file: name=/root/ansible-test/{{ pkg }} state=touch
ansible-playbook -e pkg=haha var.yaml
[websrvs:vars] #公共(組)變量 mark="-" [websrvs] #普通變量 192.168.16.115 hname=wg sname=115 192.168.16.118 hname=wg sname=118
ansible websrvs -m hostname -a 'name= {{ hname }}{{ mark }}16{{ mark }}{{sname}}'
文本文件,嵌套有腳本(使用模板編程語言編寫)
Jinja2語音,使用 字面量,有下面形式
字符串:使用單引號或者雙引號
數字:整數、浮點數
列表:[haha,xixi,hehe,lele]
元組:(haha,xixi,hehe,lele)
字典:{name:wkx,gender:male}
布爾型:true、false
算術運算:+、-、*、/、//、%、** 分別是加減乘除整除求餘冪
比較操做:==,!=,>,<,>=,<=
邏輯運算:and,or,not
流表達式:for,if,when
cat var.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: "create file" file: name="/root/ansible-test/xixi" state=touch when: ansible_os_family == "RedHat"
- hosts: 192.168.16.115 remote_user: root tasks: - name: "create some file" file: name=/root/ansible-test/{{ item }} state=touch #建立文件,嵌套變量在下面 when: ansible_distribution_major_version == "7" #匹配條件,條件知足時執行 with_items: - haha - xixi - lele - name: install some packages yum: name={{ item }} with_items: - lrzsz - net-tools
示例2:
cat user.yaml - hosts: 192.168.16.115 remote_user: root tasks: - name: "create some group" group: name={{ item }} when: ansible_distribution_major_version == "7" with_items: - g1 - g2 - g3 - name: cerate user user: name={{ item.name }} group={{ item.group }} with_items: - { name: "user1",group: "g1"} - { name: "user2",group: "g2"} - { name: "user3",group: "g3"}
Playbook中template for if
{% for vhost in nginx_vhosts %} server{ listen {{ vhost.listen | default(‘80 default_server’)}}; {% if vhost.server_name is defined %} Server_name{{ vhost.server_name }}; {% endif %} {% if vhost.root is defined %} root {{ vhost.root }} {% endif %}
示例1:
cat temnginx.yaml --- - hosts: websrvs remote_user: root vars: nginx_hosts: - 80 - 8088 - tasks: - name: templete config template: src=/root/ansible-test/nginx.conf.j2 dest=/root/nginx.conf cat nginx.conf.j2 {% for vhost in nginx_hosts %} server { listen {{ vhost }} } {% endfor %}
生成爲文件爲
cat /root/nginx.conf server { listen 80 } server { listen 8088 }
示例2:
cat temnginx.yaml --- - hosts: websrvs remote_user: root vars: nginx_hosts: - web1: listen: 8080 root: "/var/www/nginx/web1/" - web2: listen: 8080 server_name: "www.wkx.com" root: "/var/www/nginx/web2/" tasks: - name: templete config template: src=/root/ansible-test/nginx.conf.j2 dest=/root/nginx.conf cat nginx.conf.j2 {% for vhost in nginx_hosts %} server { listen {{ vhost.listen }} {% if vhost.server_name is defined %} server_name {{ vhost.server_name }} {% endif %} root {{ vhost.root }} } {% endfor %}
cat zabbix-agent.yml
- hosts: agent remote_user: root tasks: - name: copy zabbix-agent copy: src=/mnt/zabbix-agent-4.4.0-1.el7.x86_64.rpm dest=/tmp - name: install zabbix-agent yum: name=/tmp/zabbix-agent-4.4.0-1.el7.x86_64.rpm - name: copy config copy: src=/etc/zabbix/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf - name: modify conf hostname lineinfile: path: /etc/zabbix/zabbix_agentd.conf regexp: "^Hostname" line: "Hostname={{ansible_hostname}}" - name: start zabbix-agent service: name=zabbix-agent state=started enabled=yes
ansible自1.2版本後引入的新特性。用於層次性、結構性的組織playbook。Roles可以根據層次型結構自動裝載變量文件、tasks以及handlers等。要使用roles只須要在playbook中使用include指令便可。簡單來講,roles就是經過分別將變量、文件、任務、模板以及處理器放置於單獨的目錄中,並能夠便捷的include他們的一種機制。角色通常基於主機結構構建服務的場景中,但也能夠是用於構建守護進程等場景中。
複雜場景:建議使用roles,代碼複用度高
變動主機及主機組
如命名不規範維護或傳承成本高
某些功能須要多個playbook,經過include實現
Ansible的roles目錄結構
每一個角色,以特定的層級目錄結構進行組織
Roles目錄結構: playbook.yaml roles/ project/ 項目名稱,有如下子目錄 tasks/ 定義task、role的基本元素,至少包含一個名爲main.yaml的文件;其餘須要的文件在此經過include進行包含 files/ 存放由copy或者script模塊調用的文件 vars/ 不經常使用,定義變量;至少包含一個名爲main.yaml的文件;其餘須要的文件在此經過include進行包含 default/ 不經常使用,設定默認變量時使用此目錄中的main.yaml templates/ template模塊查找所須要的模板問文件的目錄 handlers/ meta/ 不經常使用,定義當前角色的特殊設定以及依賴關係;至少包含一個名爲main.yaml的文件;其餘須要的文件在此經過include進行包含