• ansible实现template管理变量


    上面几章我们已经学会了变量的使用,以及如何书写变量,但是,同学们有没有想过,简单的任务我们的确可以命令行、hosts文件或playbook中简单定义,但是这样同样也提高了耦合性,不利于更多工作或者更多同事的协同办公呢?

    而且,其实有时候我们自己定义的变量并不能随着具体的配置来进行动态调整(而template可以),并且变量我们随便定义,也不易于我们管理,那么有没有ansible有没有一个统一的向shell中function一样定义变量的位置呢?其实是有的,那就是template;那么接下来我们开始规范我们的变量,一起来了解ansible中template的使用吧

    模板templates简介
    文本文件,嵌套有脚本(使用模板编程语言编写)
    jinja2语言,使用字面量,有下面形式
    字符串:使用单引号或者双引号
    数字:整数、浮点数
    列表:[item1,item2,....]
    元组:[item1,item2,....]
    字典:{key1:value1,key2:value2,.....}
    布尔型:true/falue
    算数运算:+,-,*,/,//,%,**
    比较操作:==,!=,>,>=,<,<= #### '//':整除的意思,除完了取整 '%':取模,也叫作取余 '**':幂次方,取指数
    逻辑运算:and,or,not
    流表达式:For If When
    templates功能
    templates功能:根据模板文件动态生成对应的配置文件
    templates文件必须存放于templates目录下,且命名为.j2结尾
    并且template不能用于ad-hoc的命令行模式
    yaml/yml文件需和templates目录平级,目录结构如下:
    .
    ├── temnginx.yml
    └── templates
    └── nginx.conf.j2
    示例:我们做一个nginx的测试
    ---
    - hosts: ceshi
    remote_user: root

    tasks:
    - name: install package
    yum: name=nginx
    - name: copy template
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: start service
    service: name=nginx state=started enabled=yes
    将nginx的配置文件放在我们的templates/nginx.conf.j2里面,并以.j2结尾
    ansible-playbook temnginx.yml
    [root@ceshi ansible]# ansible -i hosts ceshi -m shell -a "ps -aux|grep nginx" ###我们看进程都已经起来了
    10.0.194.83 | CHANGED | rc=0 >>
    root 27410 0.0 0.0 120760 2228 ? Ss 16:29 0:00 nginx: master process /usr/sbin/nginx
    nginx 27411 0.0 0.0 121224 3124 ? S 16:29 0:00 nginx: worker process
    nginx 27412 0.0 0.0 121224 3124 ? S 16:29 0:00 nginx: worker process
    nginx 27413 0.0 0.0 121224 3124 ? S 16:29 0:00 nginx: worker process
    nginx 27414 0.0 0.0 121224 3124 ? S 16:29 0:00 nginx: worker process
    root 27598 0.0 0.0 113120 1192 pts/0 S+ 16:31 0:00 /bin/sh -c ps -aux|grep nginx
    root 27600 0.0 0.0 112652 940 pts/0 S+ 16:31 0:00 grep nginx


    这时我们来自定义nginx的进程数
    user nginx;
    worker_processes auto; ###一般可以自己根据主机的cpu个数进行调整,我们可以自定义
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;

    而且此处我们可以写成变量的形式,首先,我们用ansible的setup模块取出VCPU的表示
    [root@ceshi ansible]# ansible -i hosts ceshi -m setup |grep "processor"
    "ansible_processor": [
    "ansible_processor_cores": 1,
    "ansible_processor_count": 4,
    "ansible_processor_threads_per_core": 1,
    "ansible_processor_vcpus": 4,

    然后修改 worker_processes auto; ------> worker_processes {{ ansible_processor_vcpus*2 }}; ###nginx进程数乘以2
    修改完模板后,由于我们之前都已经装过包了,所以我们需要再次修改我们的playbook
    ---
    - hosts: ceshi
    remote_user: root

    tasks:
    - name: install package
    yum: name=nginx
    - name: copy template
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    notif: restart service
    - name: start service
    service: name=nginx state=started enabled=yes
    handlers:
    - name: restart service
    service: name=nginx state=restarted

    执行playbook:ansible-playbook temnginx.yml
    查看我们修改后的结果:
    [root@ceshi ansible]# ansible -i hosts ceshi -m shell -a "ps -aux|grep nginx"
    10.0.194.83 | CHANGED | rc=0 >>
    root 28301 0.0 0.0 120760 2232 ? Ss 16:51 0:00 nginx: master process /usr/sbin/nginx
    nginx 28302 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28303 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28304 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28305 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28306 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28307 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28308 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    nginx 28309 0.0 0.0 121224 3128 ? S 16:51 0:00 nginx: worker process
    root 28348 0.0 0.0 113120 1188 pts/0 S+ 16:51 0:00 /bin/sh -c ps -aux|grep nginx
    root 28350 0.0 0.0 112652 944 pts/0 S+ 16:51 0:00 grep nginx
    可以看到,已经达到我们想要的结果了

    ####变量的优先级:命令行-e指定的最高,次之为playbook中定义的,最次为hosts文件中定义的
    when条件判断的使用
    条件测试:如果需要根据变量、facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试,通过when语句实现,
    在task中使用,jinja2的语法格式。
    when语句
    在task后添加when字句即可使用条件测试;when语句还支持jinja2表达式语法
    示例:
    tasks:
    - name: "shutdown RedHat flavored systems"
    command: /sbin/shutdown -h now
    when: ansible_os_family == "RedHat"

    示例:
    [root@ceshi ansible]# ansible -i hosts ceshi -m setup -a 'filter="*distribution*"'
    10.0.194.83 | SUCCESS => {
    "ansible_facts": {
    "ansible_distribution": "CentOS",
    "ansible_distribution_file_parsed": true,
    "ansible_distribution_file_path": "/etc/redhat-release",
    "ansible_distribution_file_variety": "RedHat",
    "ansible_distribution_major_version": "7",
    "ansible_distribution_release": "Core",
    "ansible_distribution_version": "7.3.1611"
    },
    "changed": false
    }

    我们首先获取操作系统的版本,然后根据操作系统是6还是7使用when(注意记得修改我们jinja2模板)
    - hosts: ceshi
    remote_user: root

    tasks:
    - name: install package
    yum: name=nginx
    - name: copy template
    template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
    when: ansible_distribution_major_version == "7"
    notif: restart service
    - name: copy template
    template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
    when: ansible_distribution_major_version == "6"
    notif: restart service
    - name: start service
    service: name=nginx state=started enabled=yes
    handlers:
    - name: restart service
    service: name=nginx state=restarted
    迭代:with_items
    迭代:当有需要重复性执行的任务时,可以使用迭代机制
    对迭代项的引用,固定变量名为“item”
    要在task中使用with_items给定迭代的元素列表
    列表格式:
    字符串
    字典

    示例:
    ---
    - hosts: ceshi
    remote_user: root

    tasks:
    - name: create some file
    file: name=/date/{{ item }} state=touch
    when: ansible_distribution_major_version == "7"
    with_items:
    -file1
    -file2
    -file3
    - name: install some package
    yum: name={{ item }}
    with_items:
    - htop
    - sl
    - hping3

    示例:迭代嵌套子变量
    - hosts: ceshi
    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' }

    playbook中template for if
    { % for vhost in nginx_vhosts % }
    server {
    listen {{ vhost.listen |default('80 default_server') }};

    {% if vhost.server_name is defind %}
    server_name {{ vhost.server_name }};
    { % endif % }

    {% if vhost.root is defind %}
    root {{ vhost.root }};
    {% endif %}


    示例1、:
    ---
    - host: ceshi
    remote_user: root
    vars:
    ports:
    - 81
    - 82
    - 83
    tasks:
    - name: copy conf
    template: src=for1.conf.j2 dest=/data/for1.conf
    然后cd到templates目录下编辑for1.conf.j2
    {% for port in ports %}
    server{
    listen {{ port }}
    }
    {% endfor %}
    当然也可以写成字典的形式
    示例2、:
    ---
    - host: ceshi
    remote_user: root
    vars:
    ports:
    - listen_port: 81
    - listen_port: 82
    - listen_port: 83
    tasks:
    - name: copy conf
    template: src=for1.conf.j2 dest=/data/for1.conf
    然后我们的template模版也要改
    {% for port in ports %}
    server{
    listen {{ port.listen_port }}
    }
    {% endfor %}
    ————————————————
    版权声明:本文为CSDN博主「少年你是谁的英雄」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_42645775/article/details/87800239

  • 相关阅读:
    28完全背包+扩展欧几里得(包子凑数)
    HDU 3527 SPY
    POJ 3615 Cow Hurdles
    POJ 3620 Avoid The Lakes
    POJ 3036 Honeycomb Walk
    HDU 2352 Verdis Quo
    HDU 2368 Alfredo's Pizza Restaurant
    HDU 2700 Parity
    HDU 3763 CDs
    POJ 3279 Fliptile
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/16695927.html
Copyright © 2020-2023  润新知