章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • ansible系列文章

    • ansible入门
    • anisble批量安装node_exporter
    • ansible-playbook中的变量
    • Ansible性能优化——提升ansible执行效率
    • ansible之roles简单使用
    • ansible中template简单使用
      • playbook详解
      • ansible-playbook编排使用tips
      • ansible优秀案例
      • 如何优雅向chatgpt提问ansible相关问题
    • Kubernetes笔记

    • elk

    • jenkins

    • GitLabCI_CD

    • 专题
    • ansible系列文章
    章工运维
    2022-12-09
    目录

    ansible中template简单使用

    # ansible中template简单使用 (opens new window)

    目录

    • 一、模板(template)简介 (opens new window)
    • 二、使用template部署nginx (opens new window)
    • 三、playbook中when简单使用 (opens new window)
    • 四、playbook中with_items简单使用
      • 4.1 迭代:with_items (opens new window)
      • 4.2 迭代嵌套子变量 (opens new window)
    • 五、template循环示例
      • 5.1 第一种写法 (opens new window)
      • 5.2 第二种写法 (opens new window)
      • 5.3 第三种写法 (opens new window)
    • 六、playbook中if简单使用 (opens new window)

    # 一、模板(template)简介

    • 文件文件,嵌套有脚本(使用模板编程语言编写);
    • jinja2语言,使用字面量,有以下形式:
      • 字符串:使用单引号或双引号;
      • 数字:整数,浮点数;
      • 列表:[ item1,item2,……]
      • 元组:(item1,item2,……)
      • 字典:{key1:value1,key2,value2,……}
      • 布尔型:true/false
    • 算术运算:+,-,*,/,//,%,**
    • 比较操作:==,!=,>,>=,<,<=
    • 逻辑运算:and,or,not
    • 流表达式:for,if,when

    # 二、使用template部署nginx

    $ ls     //建议,安装nginx的yaml和templates目录在同一目录下
    install_nginx.yaml  templates
    $ cat install_nginx.yaml 
    ---
    - hosts: webservers
      remote_user: root
      vars:         #创建变量信息
        - http_port: 8888
    
      tasks:
        - name: install package
          yum: name=nginx
        - name: template copy
          template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf   #由于模板文件在tmplate下,所以src后的路径就可以只写配置文件名称
          notify: restart service    #定义notify便于修改文件重启服务
        - name: start service
          service: name=nginx state=started enabled=yes
    
      handlers:    #定义重启服务策略
        - name: restart service
          service: name=nginx state=restarted
    $ ls templates/     #注意模板的文件名称有一定的要求
    nginx.conf.j2
    $ cat templates/nginx.conf.j2    #该文件就是nginx的配置文件复制而成的
    …………      #省略部分内容
    worker_processes {{ ansible_processor_vcpus**2 }};    #使用变量是cpu核心数的2次方
    listen       {{ http_port }} default_server;
    listen       [::]:{{ http_port }} default_server;
    #使用playbook中定义的变量信息
    $ ansible-playbook install_nginx.yaml
    #运行playbook文件
    $ ansible webservers -m shell -a 'rpm -q nginx'
    #nginx确实已经安装成功
    $ ansible webservers -m shell -a 'ss -lnt | grep 8888' 
    #端口已经正常开启
    $ ansible webservers -m shell -a 'ps aux | grep nginx'
    #确认工作进程数是预期设定的值
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    至此template批量部署nginx已经实现!

    # 三、playbook中when简单使用

    when:可以简单理解为一个条件判断,类似于shell脚本中的if语句!

    因为ansible管理的主机可能不是一个系统版本的,那么就需要区别部署了!

    $ ansible all -m setup -a 'filter=*distribution*'
    #查看ansible默认支持的变量
    $ cat install_nginx.yaml 
    ---
    - hosts: all     #针对所有主机
      remote_user: root
      vars:
        - http_port: 8888
    
      tasks:
        - name: install package
          yum: name=nginx
        - name: template copy for centos7
          template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
          when: ansible_distribution_major_version == "7"    #当检测到系统版本为7才执行本模块的操作
          notify: restart service
        - name: template copy for centos6
          template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
          when: ansible_distribution_major_version == "6"
          notify: restart service
        - name: start service
          service: name=nginx state=started enabled=yes
    
      handlers:
        - name: restart service
          service: name=nginx state=restarted
    $ ls templates/   #注意一个是nginx6的配置文件,一个nginx7的配置文件
    nginx.conf6.j2  nginx.conf7.j2
    $ ansible-playbook install_nginx.yaml
    #执行playbook文件
    $ ansible all -m shell -a 'ss -lntp | grep nginx'
    #确认centos 6系统的nginx已经启动
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32

    # 四、playbook中with_items简单使用

    # 4.1 迭代:with_items

    迭代:with_items:当有需要重复性执行任务是,可以使用迭代机制!

    • 带迭代项的引用,固定变量为“item”;
    • 在task中使用with_items定义需要迭代的元素列表;
    • 列表格式:
      • 字符串;
      • 字典;
    $ cat test.yaml 
    ---
    - hosts: all
      remote_user: root
    
      tasks:
        - name: touch some file
          file: name=/data/{{ item }} state=touch   #文件名定义为列表元素
          when: ansible_distribution_major_version == "7"
          with_items:      #定义列表元素
            - file1
            - file2
            - file3
    $ ansible-playbook test.yaml
    $ ansible all -m shell -a 'ls -l /data'
    #当满足条件的主机都创建了文件
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    # 4.2 迭代嵌套子变量

    $ cat test1.yaml 
    ---
    - hosts: all
      remote_user: root
    
      tasks:
        - name: create some group
          group: name={{ item }}
          with_items:
            - g1
            - g2
            - g3
        - name: create some users
          user: name={{ item.name }} group={{ item.group }}
          with_items:
            - { name: 'user1', group: 'g1'}
            - { name: 'user2', group: 'g2'}
            - { name: 'user3', group: 'g3'}
    $ ansible-playbook test1.yaml
    $ ansible all -m shell -a 'getent group'
    $ ansible all -m shell -a 'getent passwd'
    $ ansible all -m shell -a 'id user1'
    #进行验证
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    # 五、template循环示例

    # 5.1 第一种写法

    $ cat test2.yaml 
    ---
    - hosts: webservers
      remote_user: root
      vars:
        ports:
          - 81
          - 82
          - 83
    
      tasks:
        - name: copy conf
          template: src=for2.conf.j2 dest=/data/for2.conf
    $ cat templates/for2.conf.j2 
    {% for port in ports %}
    server {
        listen {{ port }}
    }
    {% endfor %}
    $ ansible-playbook test2.yaml
    $ ansible webservers -m shell -a 'cat /data/for2.conf'
    #进行验证
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    # 5.2 第二种写法

    $ cat test3.yaml 
    ---
    - hosts: webservers
      remote_user: root
      vars:
        ports:
          - listen_port: 81
          - listen_port: 82
          - listen_port: 83
    
      tasks:
        - name: copy conf
          template: src=for3.conf.j2 dest=/data/for3.conf
    $ cat templates/for3.conf.j2 
    {% for port in ports %}
    server {
        listen {{ port.listen_port }}
    }
    {% endfor %}
    $ ansible-playbook test3.yaml
    $ ansible webservers -m shell -a 'cat /data/for3.conf'
    #进行验证  
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    # 5.3 第三种写法

    $ cat test4.yaml 
    ---
    - hosts: webservers
      remote_user: root
      vars:
        ports:
          - web1:
            port: 81
            name: web1.lzj.com
            rootdir: /data/web1
          - web2:
            port: 82
            name: web2.lzj.com
            rootdir: /data/web2
          - web1:
            port: 83
            name: web3.lzj.com
            rootdir: /data/web3
    
      tasks:
        - name: copy conf
          template: src=for4.conf.j2 dest=/data/for4.conf
    $ cat templates/for4.conf.j2 
    {% for p in ports %}
    server {
        listen {{ p.port }}
        servername {{ p.name }}
        documentroot {{ p.rootdir }}
    }
    {% endfor %}
    $ ansible-playbook test4.yaml
    $ ansible webservers -m shell -a 'cat /data/for4.conf'
    #进行验证
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    # 六、playbook中if简单使用

    $ cat test5.yaml 
    ---
    - hosts: webservers
      remote_user: root
      vars:
        ports:
          - web1:
            port: 81
            rootdir: /data/web1
          - web2:
            port: 82
            name: web2.lzj.com
            rootdir: /data/web2
          - web1:
            port: 83
            rootdir: /data/web3
    
      tasks:
        - name: copy conf
          template: src=for5.conf.j2 dest=/data/for5.conf
    $ cat templates/for5.conf.j2 
    {% for p in ports %}
    server {
        listen {{ p.port }}
    {% if p.name is defined%}    #如果名称被定义了才给名字赋值
        servername {{ p.name }}
    {% endif %}
        documentroot {{ p.rootdir }}
    }
    {% endfor %}
    $ ansible-playbook test5.yaml      
    $ ansible webservers -m shell -a 'cat /data/for5.conf'
    #进行验证
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    *************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************

    原文链接:https://www.cnblogs.com/lvzhenjiang/p/14199384.html

    微信 支付宝
    #ansible
    上次更新: 2024/10/22, 18:10:01

    ← ansible之roles简单使用 playbook详解→

    最近更新
    01
    不花一分钱从0到1建站教程
    04-22
    02
    批量拿取多台服务器的日志文件
    04-21
    03
    高德MCP智能体搞定旅游计划
    04-19
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式