YAML是一種表達資料序列的格式,因爲參考了其餘多種語言,因此具備很高的可讀性。其特性以下:node
1.YAML中兩種經常使用的數據類型,分別是list和directorylinux
-teacher -student
2.列表的全部元素均使用「-」開頭web
3.字典經過key和value進行標識如:sql
name:zhangsan job:teacher age:25
playbook是由一個或者多個play組成的列表,主要功能是將task定義好的角色併爲一組進行統一管理,也就是經過task調用Ansible的模塊將多個paly組織在一個playbook中。playbook自己由如下各部分組成:shell
1.下面是一個playbook的簡單示例:apache
- hosts: webserver #定義的主機組,即應用的主機 vars: #定義變量 http_port: 80 max_clients: 200 user: root tasks: #執行的任務 - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running service: name=httpd state=started handlers: #處理器 - name: restart apache service: name=httpd state=restarted
playbook的設計目的就是爲了讓某個或者某些主機以某個身份去執行相應的任務。其中用於指定要執行任務的主機hosts定義,能夠是一個主機也能夠是由冒號分隔的額多個主機組;用於指定被管理主機上執行任務的用戶用remote_user來定義,例如:centos
- hosts: webserver remote_user: root
1.remote_user也能夠定義指定用戶經過sudo的方法在被管理主機上運行指令,甚至能夠在使用become指定sudo切換的用戶。app
- hosts: webserver remote_user: root tasks: - name: ping test ping: become: yes become_user: zhangsan [root@rabbitmq01 ~]# ansible-playbook a.yml PLAY [webserver] ********************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [192.168.58.132] TASK [ping test] ********************************************************************************* [WARNING]: Module remote_tmp /home/zhangsan/.ansible/tmp did not exist and was created with a mode of 0700, this may cause issues when running as another user. To avoid this, create the remote_tmp dir with the correct permissions manually ok: [192.168.58.132] PLAY RECAP *************************************************************************************** 192.168.58.132 : ok=2 changed=0 unreachable=0 failed=0
1.Play的主體部分是task列表,task列表中的各任務按次序逐個在hosts中指定的主機上執行,即在全部主機上完成第一個任務後再開始第二個任務。
在運行playbook時(從上到下執行),若是一個host執行task失敗,整個tasks都會回滾,請修正playbook 中的錯誤,而後從新執行便可。
Task的目的是使用指定的參數執行模塊,而在模塊參數中可使用變量.ssh
2.每個task必須有一個名稱name,這樣在運行playbook時,從其輸出的任務執行信息中能夠很好的辨別出是屬於哪個task的。若是沒有定義name,‘action’的值將會用做輸出信息中標記特定的task。tcp
3.定義一個task,常見的格式:」module: options」 例如:yum: name=httpd
tasks: - name: make sure appache is running service: name=httpd state=started
4.ansible的自帶模塊中,command模塊和shell模塊無需使用key=value格式
tasks: -name: disable selinux command: /sbin/setenforce 0
Handlers用於當關注的資源發生變化時所採起的操做。在notify中列出的操做便稱爲handler,也就是在notify中須要調用handler中定義的操做。而notify這個動做在每一個play的最後被觸發,僅在全部的變化發生完成後一次性執行指定的操做。handler也是task列表的格式:
notify: - restart httpd #一旦執行這裏就會觸發name爲restart httpd的handler handlers: - name: restart httpd service: name=httpd state=restarted
Jinja是基於Python的模板引擎。Template類是Jinja的另外一個重要組件,能夠看做一個編譯過的模板文件,用於產生目標文本,傳遞Python的變量給模板去替換模板中的標記。這裏就先演示一個示例,經過ansible在兩臺被管理主機上安裝httpd服務,而且修改httpd.conf文件後,重啓。前面的安裝已經介紹過,這裏我直接說明下實驗環境便可:
角色 | 主機名 | IP地址 | 組名 |
---|---|---|---|
控制主機 | node1 | 192.168.58.146 | |
被管理主機 | node2 | 192.168.58.148 | webserver |
被管理主機 | node3 | 192.168.58.149 | sqlserver |
- hosts: webserver remote_user: root vars: #定義幾個變量 - httpdport: 192.168.58.148:80 - servername: www.yun.com:80 - service: httpd tasks: - name: install httpd service yum: name={{service}} state=latest - name: install configuration file for httpd template: src=/root/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf # 首先在本地生成一份template文件 notify: - restart httpd handlers: - name: restart httpd service: name={{service}} state=restarted
1.這個是yml文件內容,下面時模板文件中須要修改的內容
[root@node1 ~]# cat httpd.conf.j2 | grep '^Listen' Listen {{httpdport}} [root@node1 ~]# cat httpd.conf.j2 | grep '^ServerName' ServerName {{servername}}
[root@node1 ~]# ansible-playbook abc.yml PLAY [webserver] ********************************************************************************** TASK [Gathering Facts] **************************************************************************** ok: [192.168.58.148] TASK [install httpd service] ********************************************************************** changed: [192.168.58.148] TASK [install configuration file for httpd] ******************************************************* changed: [192.168.58.148] RUNNING HANDLER [restart httpd] ******************************************************************* changed: [192.168.58.148] PLAY RECAP **************************************************************************************** 192.168.58.148 : ok=4 changed=3 unreachable=0 failed=0
2.能夠看到node2上服務已經開啓,監聽端口也已經修改過來了
[root@node2 .ssh]# rpm -q httpd httpd-2.4.6-80.el7.centos.1.x86_64 [root@node2 .ssh]# netstat -ntap | grep 80 tcp 0 0 192.168.58.148:80 0.0.0.0:* LISTEN 3540/httpd [root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^Listen' Listen 192.168.58.148:80 [root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^ServerName' ServerName www.yun.com:80
若是屢次執行修改playbook會涉及到一些沒有變化的代碼,可使用tags讓用戶選擇跳過沒有變化的代碼,只運行olaybook中發生變化的部分代碼,能夠在playbook中爲某個或者某些任務定義「標籤」,在執行此playbook時經過ansible-playbook命令使用--tags選項能實現僅運行指定的tasks。
- hosts: sqlserver remote_user: root tasks: - name: build a new file copy: content="this is a test" dest=/root/test1.txt tags: - only - name: bulid another file copy: content="this is another test" dest=/root/test2.txt [root@node1 ~]# ansible-playbook a.yml --tags="only" PLAY [sqlserver] ********************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [192.168.58.149] TASK [build a new file] ************************************************************************** changed: [192.168.58.149] PLAY RECAP *************************************************************************************** 192.168.58.149 : ok=2 changed=1 unreachable=0 failed=0
咱們去node3中查看到底生成了幾個文件
[root@node3 ~]# ls anaconda-ks.cfg mfs-1.6.27-5.tar.gz 公共 視頻 文檔 音樂 initial-setup-ks.cfg test1.txt 模板 圖片 下載 桌面
能夠看到只生成了test1.txt