ansible中playbook使用

palybook使用python

####yaml語法
ansible中使用的yaml基礎元素:
變量
Inventory
條件測試
迭代

playbook的組成結構
Inventory
Modules
Ad Hoc Commands
Playbooks
        Task:任務,即調用模塊完成的某操做
        Variables:變量
        Templates:模板
        Handlers:處理器,由某事件觸發執行的操做
        Roles:角色nginx

#####定義/etc/ansible/hostsweb

兩種方式:shell

基於ssh密碼登陸apache

不配置免密碼登陸,須要在hosts文件中配置vim

第一種:基於ssh密碼登陸服務器

參數一:inventory
ansible的主要功能用於批量管理主機操做,便捷的使用部分主機,能夠在inventory file中分組
默認的inventory file爲/etc/ansible/hosts

1.inventory文件格式
同一主機歸併到一個或者多個組中,主機使用非默認的22端口,也能夠在主機名稱後加冒號端口標明app

www.ab.com
[webservers] www.abc.com:222 www.abcd.com [dbservers] db1.abc.com db2.abc.com db3.abc.com

 若是主機遵循類似的命名模式,可使用列表標記各個主機ssh

[webserver]
www[01:30].example.com

[dbserver]
db-[a:f].example.com

 2.主機變量
能夠在定義主機時添加遠程主機變量便於在playbook中使用{http中的變量,在j2中配置}測試

[webserver]
www.ab.com http_port=80 maxRequestsPerChild=808
www.abc.com http_port=8080 maxRequestsPerChild=909

 3.組變量
賦予指定組內的全部主機在playbook中用的變量(vars)
能夠調用組裏面的變量。

[webserver]
www.ab.com
www.bc.com
[webserver:vars]
host_port=80
maxRequestsPerChild=890

4.組嵌套
組還包括其餘組,能夠在其餘組中指定變量,只能在ansible-playbook中使用,ansible不支持
children中包括apache,nginx的主機。,共四臺主機

[apache]
http1.abc.com
http2.abc.com
[nginx]
nginx1.abc.com
nginx2.abc.com
[webservers:children]
apache
nginx
[webservers:vars]
host_port=80

 ansible基於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

第二種:不基於ssh免密碼登陸

即使是咱們沒有作關於祕鑰的認證,咱們也會自動使用帳戶和密碼來登陸該主機。

[webservers]
192.168.133.2 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.133.3 testvar="2.100

 ####基本結構

實例一:module_name: module_args   模塊名和模塊參數

- host: webserver
  remote_user:
  task:
  - task1
    module_name: module_args
    - name: test connection
      ping:
      remote_user; www
      sudo: yes
      command: /sbin/setenforce 0
      shell: /usr/bin/somecommand || /bin/true
      ignore_errors: True
  - task2
    module_name: module_args

- host:dbservers

 在運行playbook是中途發生錯誤,可能會回滾,更正後,從新執行一次。
能夠指定使用sudo的方式遠程執行任務,用sudo到root用戶執行命令。
衆多模塊中,只有command和shell模塊僅使用給定一個列表而無需使用"key=value"
命令或腳本的退出碼不爲0,可使用如上替代,命令不成功強行成功。
使用ignore_errors忽略錯誤信息



實例二:handlers:

  在notify中列出的操做爲handler,好比以前配置了apache的配置文件,http.conf端口發生變化後,從新執行ansible-playbook後,查看遠程的端口,並未發生變化。須要用到notify

引入變量:
    vars:在後面添加變量名,而後再引入變量,必須加{{}},用變量名替換。

- host: webservers
  remote_user: root
vars:
- packages: httpd tasks: - name: install httpd packages yum: name={{ packages }} state=lastest - name: install configuration file for httpd copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restart httpd - name: start httpd service service: enabled=true name={{ packages }} state=started handlers: - name: restart httpd service: name={{ packages }} state=restarted

 實例三:將每臺主機的ip地址(ansible_all_ipv4_addresses)發送到遠程主機的/tmp/var.ansible文檔中,查找變量名(ansible_all_ipv4_addresses用命令:ansible 192.168.133.4 -m setup ),也能夠用主機傳導一些變量。用組變量或者inventory變量。在主機webservers中定義變量testvar和調用ansible_ssh_host,在目標主機中調用這個變量。

vi /etc/ansible/hosts
[webservers]
192.168.133.2 testvar="1.100" ansible_ssh_host=root
192.168.133.3 testvar="2.100"
vi test.yml
- hosts: webservers
  remote_user: root
  tasks:
  - name: copy file
    copy: content="{{ ansible_all_ipv4_addresses }}, {{ testvar }}" dest=/tm/vars.ansible

在遠程主機上查看cat /tmp/vars.ansible
或者ssh 192.168.133.2 cat /tmp/vars.ansible

 案例四:when條件變量

當檢查系統爲Debin系統時,關機。

- name: "shutdown Debian system"
  command: /sbin/shutdown -h now
  when: ansible_os_family == "Debin"

 當ansible_fqdn == "www.test.com"時,建立user10用戶

- hosts: all
  remote_user: root
  vars:
  - username: user10
  tasks:
  - name: create {{ username }} user
    user: name={{ username }}
    when: ansible_fqdn == "www.test.com"

 案例五:迭代:
    當有須要重複性執行的任務時,能夠用迭代機制,使用格式爲將須要迭代的內容定義爲item變量引用
    並經過with_items語句來指明迭代的元素列表便可

    - name: add several users
      user: name={{ item }} state=present groups=wheel
      with_items:
         - testuser1
         - testuser2
    上面的語句功能等同於下面的語句:
    - name: add several users1
      user: name=testuser1 state=present groups=wheel
    - name: add several users2
      user: name=testuser2 state=present groups=wheel

事實上with_items可使用元素還可爲hashes,

注意:name: 'testuser1'調用的是item.name;group: 'wheel'調用的是item.groups,

其中item.後面的name是本身定義的,with_items中的子名字name是本身定義的,和item.後面定義的一致。

- name: add several users
  user: name={{ item.name }} state=present groups={{ item.groups }}
  with_items:
    - { name: 'testuser1', group: 'wheel' }
    - { name: 'testuser2', group: 'root' }

 案例六:template:模板
  好比兩臺遠程服務器要監聽的端口不一樣,maxclients不一樣,並且主機名不一樣。能夠調用模板。j2表明模板。在本地建立模板文件httpd.conf.j2,在yml中定義template

cd /root/ && mkdir templates && cp conf/httpd.conf templates/
mv templates/httpd.conf template/httpd.conf.j2
vim httpd.conf.j2
Listen        {{ http_port}}
MaxClients    {{ maxClients}}
ServerName    {{ ansbible_fqdn }}
    
vi /etc/ansible/hosts
[webservers]
192.168.133.2 http_port=80 maxClients=100
192.168.133.3 http_port=8080 maxClients=200
    
vi apache.yml
- host: webservers
  remote_user: root
  vars:
  - package: httpd
  tasks:
  - name: install httpd packages
  yum: name={{ package }} state=lastest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ package }} state=started    
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    
ansible-playbook apache.yml

 案例七:tags

屢次運行playbook時,其中某些task不須要運行,只需運行某個task,能夠單獨標記該task,在yml中指定tags。
在playbook能夠爲某個或某些任務定義爲一個標籤,在執行playbook時,經過ansible-playbook命令使用--tags選項能實現僅運行指定的tasks而非全部的。

 其中tags中的conf和service爲本身命名的。特殊:不能定義爲always

vi apache.yml
- host: webservers
  remote_user: root
  vars:
  - package: httpd
  tasks:
  - name: install httpd packages
    yum: name={{ package }} state=lastest
  - name: install configuration file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    tags:
    - conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ package }} state=started
tags:
- service handlers: - name: restart httpd service: name=httpd state=restarted ansible-playbook apache.yml --tags="conf"
ansible-playbook apache.yml --tags="service"

 案例八:roles,推薦使用

推薦使用此種模式


roles:
1.目錄名同文件名
2.目錄結構有固定格式
    files:靜態文件
    templates:Jinjia2模板文件
    tasks:至少有main.yml文件,定義各tasks
    handlers:至少一個main.yml文件,定義各handlers
    vars:至少有一個main.yml文件,定義變量
    meta:定義依賴關係等信息
3.site.yml中定義playbook,額外有其餘的yml文件。

roles能根據層次型結構目錄裝載變量文件,task已經handlers,使用roles須要在playbook中
使用include指令便可。roles經過將變量,文件,任務,模塊,處理器放置到單獨的目錄找那中

roles案例:
tree ansible_playbook
site.yml
webservers.yml
dbservers.yml
roles/
    common/
        files/
        templates/
        tasks/
        handlers/
        vars/
        meta/
    webservers/
        files/
        templates/
        tasks/
        handlers/
        vars/
        meta/
    

在playbook中,能夠這樣使用roles:
- hosts: webservers
  roles:
    - common
    - webservers
    

也能夠向 roles傳遞參數,例如:

- hosts: webservers
  roles:
    - common
    - { role: foo app instance, dir:'/opt/a, port: 5000 }
    - { role: foo app instance, dir: '/opt/b', port: 5001 }

甚至也能夠條件式地使用 roles,例如

- hosts: webservers
  roles
I   - { role: some_role, when:"ansible_os_ family =='RedHat'" }

 1.建立role的步驟
(1)建立以 roles命名的目錄
(2)在 roles目錄中分別建立以各角色名稱命名的目錄,如 webservers等
(3)在每一個角色命名的目錄中分別建立f11es、 handlers、meta、 tasks、 templates和vars目錄:用不到的目錄能夠建立爲空目錄,也能夠不建立
(4)在p1 abook文件中,調用各角色
2.role內各目錄中可用的文件
tasks目錄:至少應該包含一個名爲man.ym1的文件,其定義了此角色的任務列表:此文件可使用 include包含其它的位於此目錄中的task文件;
fi1es目錄,存放由copy或script等模塊調用的文件
templates目錄: template模塊會自動在此目錄中尋找]inja2模板文件
handlers目錄:此目錄中應當包含一個main
ym1文件,用於定義此角色用到的各 handler;在 handler中使用 include包含的其它的 handler文件也應該位於此目錄中
vars目錄:應當包含一個main,ym1文件,用於定義此角色用到的變量
meta目錄:應當包含一個main,ym1文件,用於定義此角色的特殊設定及其依賴關係; ansi1e1.3及其之後的版本才支持
default目錄:爲當前角色設定默認變量時使用此目錄:應當包含一個main.ym1文件


Tags
tags用於讓用戶選擇運行或路過 playbook中的部分代碼。 ansi1e具備冪等性,所以會自動跳過沒有變化的部分,即使如此,有些代碼爲測試其確實沒有發生
變化的時間依然會很是地長。此時,若是確信其沒有變化,就能夠經過tags跳過此些代碼片段

案例:172.168.100.1配置成webserver,172.168.100.2配置成dbservers,172.168.100.3配置成web和db

mkdir -pv ansible_playbook/roles/{webservers,dbservers}/{tasks,files,templates,meta,handlers,vars}
cd roles/webservers/
cp /etc/httpd/conf/httpd.conf files/
vim tasks/main.yml
- name: install httpd packages
- yum: name=httpd
- nane: install configuration file
- copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  tags:
  - conf
  notify:
  - restart httpd
- name: start httpd
  service: name=httpd state=started

vim handler/main.yml
- name: restart httpd
- service: name=httpd state=restarted
若是有vars能夠配置,此示例中不使用
vim vars/main.yml
- http_port: 80
- maxClients: 200

若是想掉用webservers,在roles目錄以外建立site.yml文件
vim ansible_playbook/site.yml
- hosts: webservers
  remote_user: root
  roles:
  - webservers
 
- hosts: 172.168.100.1
  remote_user: root
  roles:
  - dbservers

- hosts: 172.168.100.2
  remote_user: root
  roles:
  - dbservers  
  - webservers

 同理在dbservers進行配置。ansible-playbook site.yml

相關文章
相關標籤/搜索