1. 程式人生 > >Ansible之 模板,條件測試,循環語句 使用

Ansible之 模板,條件測試,循環語句 使用

ansible 模板 條件測試 循環語

1 概述


本文將結合例子介紹模塊,條件測試和循環語句的使用


2 模板


模塊和模板的區別,template模塊是將模板文件裏的變量以實際的值重新生成後文件後,復制到遠程主機上

模塊:template

基於模板(以後綴.j2結尾的文件)方式生成一個文件復制到遠程主機,調用ansible的收集到的內建變量,變量的嵌入代碼的標誌是雙花括號

*src=

*dest=

owner=

group=

mode=

模板:templates

文本文件,嵌套有腳本(使用模板編程語言Jinja2編寫)

字面量:

字符串:使用單引號或雙引號;

數字:整數,浮點數;

列表:[item1, item2, ...]

元組:(item1, item2, ...)

字典:{key1:value1, key2:value2, ...}

布爾型:true/false

算術運算:

+, -, *, /, //, %, **

比較操作:

==, !=, >, >=, <, <=

邏輯運算:

and, or, not

例一:

安裝redis軟件,配置redis的配置文件為模板

修改配置模板文件,以.j2結尾

vim /root/redis.conf.j2

maxmemory {{ ansible_memtotal_mb / 2 }}mb

註意,template不能再命令行執行,即不能在命令裏用-m指定模塊為template執行,需要定義在腳本裏,用playbook執行腳本

vim /root/ansible/installvar.yml 
- hosts: websrvs
  remote_user: root
  vars:
  - pkgname: redis
  tasks:
  - name: install package
    yum: name={{ pkgname }} state=latest
  - name: install redis conf
    template: src=/root/redis.conf.j2 dest=/etc/redis.conf owner=redis group=root mode=644

執行腳本如下

ansible-playbook installvar.yml

則websrvs組裏的主機安裝redis後配置文件配置的maxmemory為ansible_memtotal_mb / 2 的值

例二:

安裝nginx,把nginx配置文件配置為模板


vim /root/ansible/instnginx.yml
    - hosts: websrvs
    remote_user: root
    tasks:
    - name: install nginx
     yum: name=nginx state=present
    - name: install conf file
     template: src=/root/nginx.conf.j2 dest=/etc/nginx/nginx.conf
     notify: restart nginx
     tags: instconf
    - name: start nginx service
     service: name=nginx state=started
     handlers:
    - name: restart nginx
     service: name=nginx state=restarted

模板配置文件 :nginx.conf.j2

    worker_processes {{ ansible_processor_vcpus }};
    server_name   {{  ansible_fqdn }};


3 條件測試


如被管控的主機系統不一樣,執行的語句可能會不一樣,需要做條件的判定

when語句:在task中使用,jinja2的語法格式

例子:

在centos6和centos7上安裝nginx,判斷對應主機系統版本,實現不一樣的配置

vim instnginx.yml
- hosts: nginx
  remote_user: root
  tasks:
  - name: install nginx package
    yum: name=nginx state=latest
    tags: instpkg
  - name: install conf file6
    template: src=/root/nginx.conf.c6.j2 dest=/etc/nginx/nginx.conf
    when: ansible_distribution_major_version == "6" 
    notify: restart nginx
  - name: install conf file7
    template: src=/root/nginx.conf.c7.j2 dest=/etc/nginx/nginx.conf
    when: ansible_distribution_major_version == "7" 
    notify: restart nginx
  - name: start nginx service
    service: name=nginx enabled=true state=started
  handlers:
  - name: restart nginx
    service: name=nginx state=restarted

運行

ansible-playbook instnginx.yml

例子:

根據主機的ip,拷貝不同的文件到對應的主機

vim   cpfile.yml
- hosts: nginx
  remote_user: root
  tasks:
  - name: copy file6
    copy: src=/root/nginx.conf.c6.j2 dest=/tmp/
    when: ansible_default_ipv4['address'] == '172.18.50.63'
  - name: copy file7
    copy: src=/root/nginx.conf.c7.j2 dest=/tmp/
    when: ansible_default_ipv4['address'] == '172.18.50.75'

執行


ansible-playbook cpfile.yml


4 循環


叠代,需要重復執行的任務;

1 對叠代項的引用,固定變量名為”item“

2 然後在task中使用with_items給定要叠代的元素列表;

列表方法:

字符串

字典:元素可以是字典,復制多個文件時,可以直接使用copy模塊

例子:

通過叠代的方法安裝服務包和創建用戶

vim iteration.yml
- hosts: dbsrvs
  remote_user: root
  vars: 
  - jdk_version: 1.8.0
  tasks:
  - name: install {{ item }} package
    yum: name={{ item.name }}-{{ item.version }} state=latest
    with_items:
    - { name: 'nginx',version: '1.10.2' }
    - { name: 'tree',version: '1.5.3' }
  - name: copy nginx conf
    copy: src={{ item.file }} dest={{ item.conf }}
    with_items:
    - { file: '/root/nginx.conf.os6',conf: '/etc/nginx.conf' }
  - name: install {{ item }} package
    yum: name={{ item }} state=latest
    with_items:
    - java-{{ jdk_version }}-openjdk
    - tomcat
    - tomcat-webapps
    - tomcat-docs-webapp
    - tomcat-admin-webapps
  - name: add some groups
    group: name={{ item }} state=present
    with_items:
    - ansigrp1
    - ansigrp2
  - name: add some users
    user: name={{ item.name }} group={{ item.group }} state=present
    with_items:
    - { name: 'ansiuser1', group: 'ansigrp1' }
    - { name: 'ansiuser2', group: 'ansigrp2' }


Ansible之 模板,條件測試,循環語句 使用