Ansible模版初步认识

模板
基于模板的方式生成一个文件复制到远程主机。基于ansible的templates模块实现。
将模板文件中的变量替换成变量的值后复制到远程主机。

模板:templates
文本文件,嵌套有脚本(使用模板编程语言编写)
    Jinja2:
        字面量:
                字符串:使用单引号或双引号;
                数字:整数,浮点数;
                列表:[item1, item2, ...]
                元组:(item1, item2, ...)
                字典:{key1:value1, key2:value2, ...}
                布尔型:true/false
算术运算:
    +, -, *, /, //, %, **

比较操作:
    ==, !=, >, >=, <, <=

逻辑运算:
    and, or, not 
示例: 
- hosts: websrvs
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: install conf file
template: src=files/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 }};
listen {{ http_port }}; 

例子:现在我们需要把每一台服务器的一般的内存分配给redis,但是每台服务器的
内存大小都不相同。
首先,我们在源配置文件里面修改

[ root@node1 ~ ]# cat redis.conf.j2 
# maxmemory <bytes>

maxmemory {{ ansible_memtotal_mb /2 }}mb

然后编辑一个yaml文件

[ root@node1 ~ ]# cat redis.conf.yaml 
- hosts: dbserver
remote_user: root
tasks:
- name: install redis conf
template: src=/root/redis.conf.j2 dest=/etc/redis.conf owner=redis group=root mode=644
[ root@node1 ~ ]# ansible-playbook  redis.conf.yaml 

======================================================

条件测试:
    when语句:在task中使用,jinja2的语法格式
        tasks: 
        - name: install conf file to centos7
          template: src=files/nginx.conf.c7.j2
          when: ansible_distribution_major_version == "7"
        - name: install conf file to centos6
          template: src=files/nginx.conf.c6.j2
          when: ansible_distribution_major_version == "6"               
循环:迭代,需要重复执行的任务;
    对迭代项的引用,固定变量名为”item“
    而后,要在task中使用with_items给定要迭代的元素列表;
        列表方法:
            字符串
            字典

例子:字符串迭代的方法

[ root@node1 ~ ]# cat tomcat.yaml 
- hosts: webserver
  remote_user: root
  vars:
  - jdk_version: 1.8.0
  tasks:
  - name: install {{ item }} package
    yum: name={{ item }} state=latest
    with_items:
    - nginx
    - java-{{ jdk_version }}-openjdk
    - tomcat
    - tomcat-webapps
    - tomcat-docs-webapp
    - tomcat-admin-webapps

例子:字典

[ root@node1 ~ ]# cat tomcat.yaml 
- hosts: webserver
  remote_user: root
  tasks:
  - name: install {{ item }} package
    yum: name={{ item.name }}-{{ item.version }} state=latest
    with_items:
    - { name: 'nginx', version: '1.10.2' }
    - { name: 'tomcat', version: '7.0.69' }

例子:

[ root@node1 ~ ]# cat tom.yaml 
- hosts: webserver
  remote_user: root
  vars:
  - jdk_version: 1.8.0
  tasks:
  - name: install {{ item }} package
    yum: name={{ item }} state=latest
    with_items:
    - nginx
    - java-{{ jdk_version }}-openjdk
    - tomcat
    - tomcat-webapps
    - tomcat-docs-webapp
    - tomcat-admin-webapps
  - name: install conf file
    copy: src={{ item.file }} dest={{ item.conf }}
    with_items:
    - { file: '/root/tomcat-users.xml', conf: '/etc/tomcat/tomcat-users.xml' }
    - { file: '/root/server.xml', conf: '/etc/tomcat/server.xml' }    

案例:

- name: install some packages
  yum: name={{ item }} state=present
  with_items:
  - nginx
  - memcached
  - php-fpm

- name: add some groups
  group: name={{ item }} state=present
  with_items:
  - group11
  - group12
  - group13
- name: add some users
  user: name={{ item.name }} group={{ item.group }} state=present
  with_items:
  - { name: 'user11', group: 'group11' }
  - { name: 'user12', group: 'group12' }
  - { name: 'user13', group: 'group13' }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值