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
}

你可能感兴趣的:(Ansible - - Templates 模块管理)