ansible中template簡單使用


一、模板(template)簡介

  • 文件文件,嵌套有腳本(使用模板編程語言編寫);
  • jinja2語言,使用字面量,有以下形式:
    • 字符串:使用單引號或雙引號;
    • 數字:整數,浮點數;
    • 列表:[ item1,item2,……]
    • 元組:(item1,item2,……)
    • 字典:{key1:value1,key2,value2,……}
    • 布爾型:true/false
  • 算術運算:+,-,*,/,//,%,**
  • 比較操作:==,!=,>,>=,<,<=
  • 邏輯運算:and,or,not
  • 流表達式:for,if,when

二、使用template部署nginx

$ ls     //建議,安裝nginx的yaml和templates目錄在同一目錄下
install_nginx.yaml  templates
$ cat install_nginx.yaml 
---
- hosts: webservers
  remote_user: root
  vars:         #創建變量信息
    - http_port: 8888

  tasks:
    - name: install package
      yum: name=nginx
    - name: template copy
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf   #由於模板文件在tmplate下,所以src后的路徑就可以只寫配置文件名稱
      notify: restart service    #定義notify便於修改文件重啟服務
    - name: start service
      service: name=nginx state=started enabled=yes

  handlers:    #定義重啟服務策略
    - name: restart service
      service: name=nginx state=restarted
$ ls templates/     #注意模板的文件名稱有一定的要求
nginx.conf.j2
$ cat templates/nginx.conf.j2    #該文件就是nginx的配置文件復制而成的
…………      #省略部分內容
worker_processes {{ ansible_processor_vcpus**2 }};    #使用變量是cpu核心數的2次方
listen       {{ http_port }} default_server;
listen       [::]:{{ http_port }} default_server;
#使用playbook中定義的變量信息
$ ansible-playbook install_nginx.yaml
#運行playbook文件
$ ansible webservers -m shell -a 'rpm -q nginx'
#nginx確實已經安裝成功
$ ansible webservers -m shell -a 'ss -lnt | grep 8888' 
#端口已經正常開啟
$ ansible webservers -m shell -a 'ps aux | grep nginx'
#確認工作進程數是預期設定的值

至此template批量部署nginx已經實現!

三、playbook中when簡單使用

when:可以簡單理解為一個條件判斷,類似於shell腳本中的if語句!

因為ansible管理的主機可能不是一個系統版本的,那么就需要區別部署了!

$ ansible all -m setup -a 'filter=*distribution*'
#查看ansible默認支持的變量
$ cat install_nginx.yaml 
---
- hosts: all     #針對所有主機
  remote_user: root
  vars:
    - http_port: 8888

  tasks:
    - name: install package
      yum: name=nginx
    - name: template copy for centos7
      template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"    #當檢測到系統版本為7才執行本模塊的操作
      notify: restart service
    - name: template copy for centos6
      template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
      notify: restart service
    - name: start service
      service: name=nginx state=started enabled=yes

  handlers:
    - name: restart service
      service: name=nginx state=restarted
$ ls templates/   #注意一個是nginx6的配置文件,一個nginx7的配置文件
nginx.conf6.j2  nginx.conf7.j2
$ ansible-playbook install_nginx.yaml
#執行playbook文件
$ ansible all -m shell -a 'ss -lntp | grep nginx'
#確認centos 6系統的nginx已經啟動

四、playbook中with_items簡單使用

4.1 迭代:with_items

迭代:with_items:當有需要重復性執行任務是,可以使用迭代機制!

  • 帶迭代項的引用,固定變量為“item”;
  • 在task中使用with_items定義需要迭代的元素列表;
  • 列表格式:
    • 字符串;
    • 字典;
$ cat test.yaml 
---
- hosts: all
  remote_user: root

  tasks:
    - name: touch some file
      file: name=/data/{{ item }} state=touch   #文件名定義為列表元素
      when: ansible_distribution_major_version == "7"
      with_items:      #定義列表元素
        - file1
        - file2
        - file3
$ ansible-playbook test.yaml
$ ansible all -m shell -a 'ls -l /data'
#當滿足條件的主機都創建了文件

4.2 迭代嵌套子變量

$ cat test1.yaml 
---
- hosts: all
  remote_user: root

  tasks:
    - name: create some group
      group: name={{ item }}
      with_items:
        - g1
        - g2
        - g3
    - name: create some users
      user: name={{ item.name }} group={{ item.group }}
      with_items:
        - { name: 'user1', group: 'g1'}
        - { name: 'user2', group: 'g2'}
        - { name: 'user3', group: 'g3'}
$ ansible-playbook test1.yaml
$ ansible all -m shell -a 'getent group'
$ ansible all -m shell -a 'getent passwd'
$ ansible all -m shell -a 'id user1'
#進行驗證

五、template循環示例

5.1 第一種寫法

$ cat test2.yaml 
---
- hosts: webservers
  remote_user: root
  vars:
    ports:
      - 81
      - 82
      - 83

  tasks:
    - name: copy conf
      template: src=for2.conf.j2 dest=/data/for2.conf
$ cat templates/for2.conf.j2 
{% for port in ports %}
server {
	listen {{ port }}
}
{% endfor %}
$ ansible-playbook test2.yaml
$ ansible webservers -m shell -a 'cat /data/for2.conf'
#進行驗證

5.2 第二種寫法

$ cat test3.yaml 
---
- hosts: webservers
  remote_user: root
  vars:
    ports:
      - listen_port: 81
      - listen_port: 82
      - listen_port: 83

  tasks:
    - name: copy conf
      template: src=for3.conf.j2 dest=/data/for3.conf
$ cat templates/for3.conf.j2 
{% for port in ports %}
server {
	listen {{ port.listen_port }}
}
{% endfor %}
$ ansible-playbook test3.yaml
$ ansible webservers -m shell -a 'cat /data/for3.conf'
#進行驗證  

5.3 第三種寫法

$ cat test4.yaml 
---
- hosts: webservers
  remote_user: root
  vars:
    ports:
      - web1:
        port: 81
        name: web1.lzj.com
        rootdir: /data/web1
      - web2:
        port: 82
        name: web2.lzj.com
        rootdir: /data/web2
      - web1:
        port: 83
        name: web3.lzj.com
        rootdir: /data/web3

  tasks:
    - name: copy conf
      template: src=for4.conf.j2 dest=/data/for4.conf
$ cat templates/for4.conf.j2 
{% for p in ports %}
server {
	listen {{ p.port }}
	servername {{ p.name }}
	documentroot {{ p.rootdir }}
}
{% endfor %}
$ ansible-playbook test4.yaml
$ ansible webservers -m shell -a 'cat /data/for4.conf'
#進行驗證

六、playbook中if簡單使用

$ cat test5.yaml 
---
- hosts: webservers
  remote_user: root
  vars:
    ports:
      - web1:
        port: 81
        rootdir: /data/web1
      - web2:
        port: 82
        name: web2.lzj.com
        rootdir: /data/web2
      - web1:
        port: 83
        rootdir: /data/web3

  tasks:
    - name: copy conf
      template: src=for5.conf.j2 dest=/data/for5.conf
$ cat templates/for5.conf.j2 
{% for p in ports %}
server {
	listen {{ p.port }}
{% if p.name is defined%}    #如果名稱被定義了才給名字賦值
	servername {{ p.name }}
{% endif %}
	documentroot {{ p.rootdir }}
}
{% endfor %}
$ ansible-playbook test5.yaml	  
$ ansible webservers -m shell -a 'cat /data/for5.conf'
#進行驗證


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM