zoukankan      html  css  js  c++  java
  • Ansible playbook

    一. 为什么引入playbook

    我们完成一个任务,例如安装部署一个httpd服务,我们需要多个模块(一个模块也可以称之为task)提供功能来完成。而playbook就是组织多个task的容器,他的实质就是一个文件,有着特定的组织格式,它采用的语法格式是YAML(Yet Another Markup Language)。YAML语法能够简单的表示散列表,字典等数据结构。具体请参考YAML详细语法

    参考:https://blog.51cto.com/13589448/2068546

    二. playbook基础组件

    • Hosts:运行执行任务(task)的目标主机
    • remote_user:在远程主机上执行任务的用户
    • tasks:任务列表
    • handlers:任务,与tasks不同的是只有在接受到通知时才会被触发
    • templates:使用模板语言的文本文件,使用jinja2语法。
    • variables:变量,变量替换{{ variable_name }}

    三. playbook调用方式

     ansible-playbook [options] playbook.yml [playbook2 ...]
       -C, --check # 检查但是不做真正改变
       -f FORKS, --forks=FORKS # 指定并发
       --list-hosts # 列出符合条件的主机
       --syntax-check # 语法校验
       -e EXTRA_VARS, --extra-vars=EXTRA_VARS # 传值

    应用

    - hosts: webservers     # 用户组
      remote_user: root     # 远程用户
      tasks:          
      - name: install httpd   # 任务名称                           
        yum: name=httpd state=present  # 模块 安装
      - name: install configure file
        copy: src=httpd.conf dest=/etc/httpd/conf/
      - name: start httpd service
        service: name=httpd state=started

    when

    hosts: web
    tasks:
    - name:偷看姑娘
      dong: 偷看姑娘
      when: 站着
    - name: 偷看姑娘
      dong: 偷看姑娘
      when: 趴着
    - hosts: web
      tasks:
      - name: content
        copy: content="xxxxxxxx" dest=/tmp/x.txt
        when: data=="3"
      - name: content
        copy: content="yyyyyyyy" dest=/tmp/x.txt
        when: data=="4"
    ansible-playbook -e data=3 p6.yml 

    使用playbook同时创建ann1,ann2, ann3用户

    - hosts: web
      tasks:
      - name: createuser
        user: name={{item}}
        with_items:
        - ann1
        - ann2
        - ann3

    循环  with_items:  

    - hosts: web
      tasks:
      - name: createuser
        user: name={{item}}
        with_items:
        - ann32
        - ann33
        - ann34
      - name: creategroup
        group: name={{item}}
        with_items:
        - ben23
        - ben24
        - ben25

    需求

    • 创建用户组,ben33,ben34 , ben35

    • 创建用户,ann53 ,附加组是ben33 , ann54 附加组是ben34, an55 附加组是ben35

    - hosts: web
      tasks:
      - name: creategroup
        group: name={{item}}
        with_items:
        - ben33
        - ben34
        - ben35
      - name: createuser
        user: name={{item.user}} groups={{item.group}}
        with_items:
        - {"user":ann53,"group":ben33}
        - {"user":ann54,"group":ben34}
        - {"user":ann55,"group":ben35}

    template

    - hosts: web
      tasks:
      - name: installredis
        yum: name=redis
      - name: copyfile
        template: dest=/etc/redis.conf src=redis.conf
        tags: copyfile
      - name: startredis
        service: name=redis state=restarted
    ansible-playbook -t copyfile p12.yml  创建一个tempaltes目录用来放文件

    tags

    - hosts: web
      tasks:
      - name: installredis
        yum: name=redis
      - name: copyfile
        copy: dest=/etc/redis.conf src=/root/playbook/redis.conf
        tags: copyfile
      - name: startredis
        service: name=redis state=restarted
    ansible-playbook -t copyfile p12.yml 

    handlers

    - hosts: web
      tasks:
      - name: installredis
        yum: name=redis
      - name: copyfile
        template: dest=/etc/redis.conf src=redis.conf
        tags: copyfile
        notify: restartredis
      - name: startredis
        service: name=redis state=started
      handlers:
      - name: restartredis
        service: name=redis state=restarted
  • 相关阅读:
    国外程序猿整理的机器学习资源大全
    一个改动配置文件的linux shell script
    python高精度浮点型计算的诡异错误
    错误:'dict' object is not callable
    AssertionError while merging cells with xlwt (Python)
    Python => ValueError: unsupported format character 'Y' (0x59)
    [转]Python的3种格式化字符串方法
    python requirements使用方法
    conda虚拟环境实践
    迭代器中next()的用法
  • 原文地址:https://www.cnblogs.com/niuli1987/p/10560271.html
Copyright © 2011-2022 走看看