Ansible - - Templates 模塊管理

1,templates 使用規則

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

2, when

  • 條件測試:若是須要根據變量、facts 或此前任務的執行結果來作爲某 task 執行與否的前提時要用到條件測試,經過 when 語句實現,在 task 中使用,jinja2 的語法格式
  • when 語句
  • 在 task 後添加 when 子句便可使用條件測試;when 語句支持 jinja2 表達式語法
  • 示例
- name: "shutdown RedHat flavored systems"
  command: /sbin/shutdown -h now
  when: ansible_os_family == "RedHat"
  • 示例2:若是操做系統版本爲 centos7 或 6,copy 對應文件
---
- hosts: websrvs
  remote_user: root

  tasks:
    - name: add user nginx
      user: name=nginx state=present group=nginx
    - name: Install Nginx
      yum: name=nginx state=present
    - name: copy conf file to centos7
      template: src=nginx.conf.c7.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
    - name: copy conf file to centos6
      template: src=nginx.conf.c6.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
    - name: start  Nginx
      service: name=nginx state=started enabled=ture

3, 迭代:with_items

  • 迭代:當有須要重複執行的任務時,能夠使用迭代機制
    • 對迭代項的引用,固定變量名爲 item
    • 要在 task 中使用 with_items 給定要迭代的元素列表
    • 列表格式:
      • 字符串
      • 字典
  • 示例
---
- hosts: websrvs
  remote_user: root

  tasks:
    - name: create some files
      file: name=/tmp/{{ item }}  state=touch
      with_items:
        - file1
        - file2
        - file3
  • 迭代嵌套子變量
---
- hosts: websrvs
  remote_user: root
  tasks:
    - name: add some groups
      group: name={{ item }} state=present
      with_items:
        - group1
        - group2
        - group3
    - name: add some users
      user: name={{ item.name }} group={{ item.group }} state=present
      with_items:
        - { name: 'user1', group: 'group1' }  
        - { name: 'user2', group: 'group2' }  
        - { name: 'user3', group: 'group3' }

4,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 %}
}
{% endfor %}

4.1 for 示例

  • 列表方式
// testfor1.yml
---
- hosts: websrvs
  remote_user: root
  vars:
    ports:
      - 81
      - 82
  tasks:
    - name: copy conf
      template: src=for1.conf.j2 dest=/tmp/for1.conf
// templates/for1.conf.j2
{% for port in ports %}
server {
    listen {{ port }}
}
{% endfor %}
// 執行結果
server {
    listen 81
}
server {
    listen 82
}
  • 字典方式
// testfor2.yml
---
- hosts: websrvs
  remote_user: root
  vars:
    ports:
      - listen_port: 83
      - listen_port: 84

  tasks:
    - name: copy conf
      template: src=for1.conf.j2 dest=/tmp/for2.conf
// templates/for2.conf.j2
{% for port in ports %}
server {
    listen {{ port.listen_port }}
}
{% endfor %}
// 執行結果
server {
    listen 83
}
server {
    listen 84
}
  • 多個鍵值對
// testfor3.yml
---
- hosts: websrvs
  remote_user: root
  vars:
    ports:
      - web1:
        port: 85
        name: web1.test.com
        rootdir: /data/website1
      - web2:
        port: 86
        name: web2.test.com
        rootdir: /data/website2

  tasks:
    - name: copy conf
      template: src=for3.conf.j2 dest=/tmp/for3.conf
// templates/for3.conf.j2
{% for i in ports %}
server {
    listen {{ i.port }}
    servername: {{ i.name }}
    documentroot: {{ i.rootdir }}
}
{% endfor %}
// 執行結果
server {
    listen 85
    servername: web1.test.com
    documentroot: /data/website1
}
server {
    listen 86
    servername: web2.test.com
    documentroot: /data/website2
}

4.1 if 示例

  • for 嵌套 if
// testfor4.yml
---
- hosts: websrvs
  remote_user: root
  vars:
    ports:
      - web1:
        port: 85
        name: web1.test.com
        rootdir: /data/website1
      - web2:
        port: 86
       # name: web2.test.com
        rootdir: /data/website2

  tasks:
    - name: copy conf
      template: src=for4.conf.j2 dest=/tmp/for4.conf
// templates/for4.conf.j2
{% for i in ports %}
server {
        listen {{ i.port }}
        {% if i.name is defined %}
        servername: {{ i.name }}
        {% endif %}
        documentroot: {{ i.rootdir }}
}
{% endfor %}
// 執行結果
server {
    listen 85
        servername: web1.test.com
        documentroot: /data/website1
}
server {
    listen 86
        documentroot: /data/website2
}
相關文章
相關標籤/搜索