zoukankan      html  css  js  c++  java
  • ansible-playbook剧本

    Playbooks 是一种简单的配置管理系统与多机器部署系统的基础, 非常适合于复杂应用的部署

    由 yaml 语言编写, 运行过程中, ansible-playbook 命令根据自上而下的顺序依次执行

    playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过Ansible中的tasks定义好任务。从根本上来讲所谓tasks无非是调用Ansible的一个module。将多个“play”组织在一个playbook中即可以让它们联同起来按事先编排的机制一同工作。

    ansible-playbook -h :
    ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check         # 检查,白跑,干跑 -f FORKS, --forks=FORKS # 用来做并发 --list-hosts          # 列出主机列表 --syntax-check         # 语法检查

     

    Playbook 构成

    Playbook主要有以下四部分构成:

    1. target section:定义将要执行playbook的远程主机组
    2. variable section:定义playbook运行时需要使用的变量
    3. task section:定义将要在远程主机上执行的任务列表
    4. handler section:定义task执行完成以后需要调用的任务

    第一个简单的用法: 

    YAML对空格非常敏感,并使用空格来将不同的信息分组在一起,在整个文件中应该只使用空格而不使用制表符,并且必须使用一致的间距,才能正确读取文件。相同缩进级别的项目被视为同级元素。

    字典 :

    列表 -

    后缀名 yml / yaml

    -  : 后面必须有一个空格     = 等号两侧不能有空格

    - hosts: web
      tasks:
      - name: creategroup
        group: name=may1
    - name: cretaeuser user: name=may1

    传参

    - hosts: web
      tasks:
      - name: create{{ user }}
        user: name={{ user}}

    方式一: 命令行-e

    ansible-playbook -e 'user=ryan2' p2.yml

    方式二: 修改/etc/ansible/hosts 文件

    [db:vars] #表示组的参数
    user=ryan3

    方式三:  编辑yml文件

    - hosts: db
      vars:
        - user: alexsb14
      tasks:
        - name: create{{ user }}
          user: name={{ user}}

    方式四: register

    - hosts: db
      tasks:
      - name: sum
        shell: echo 2+3|bc
        register: user
      - name: createuser
        user: name={{user.stdout}}

    优先级

    -e > playbook,yml > hosts文件

    条件判断: 

    xxx.yml

    - hosts: db
      remote_user: root
      tasks:
      - name: createfile
        copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
        when: a=="3"
      - name: cratefile
        copy: content="小弦切切如私语" dest=/tmp/a.txt
        when: a=="4"

    命令: ansible-playbook -e 'a=4'  xxx.yml

    循环:

    xxx.yml

    - hosts: web
      tasks:
      - name: crate groups
        group: name={{item}}
        with_items:
        - overwatch1
        - overwatch2
        - overwatch3
      - name: createuser
        user: name={{item.name}} group={{item.group}}
        with_items:
        - {'name':genji,'group':overwatch1}
        - {'name':mccree,'group':overwatch2}
        - {'name':hanzo,'group':overwatch3}

    命令: ansible-playbook   xxx.yml

    标签:

    xxx.yml

    - hosts: web
      tasks:
      - name: install nginx
        yum: name=nginx
      - name: copy file
        copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
        tags: copyfile
      - name: start
        service: name=nginx state=started

    命令: ansible-playbook -t copyfile xxx.yml

    template

    templateh和copy 区别

    1. template模块代替参数,渲染变量

    - hosts: web
      tasks:
      - name: install redis
        yum: name=redis
      - name: copyfile
        template: src=/etc/redis.conf dest=/etc/redis.conf
      - name: start
        service: name=redis state=started
      
    # 配置文件: bind {{ ansible_default_ipv4.address }}

    2. template可以写相对路径, 在当前工作目录下新建templates文件夹, 然后吧文件放templates文件夹里

    - hosts: web
      tasks:
      - name: install redis
        yum: name=redis
      - name: copyfile
        template: src=redis.conf dest=/etc/redis.conf
      - name: start
        service: name=redis state=started

    handlers : 触发任务, 和 notify搭配使用

    - hosts: web
      tasks:
      - name: install redis
        yum: name=redis
      - name: copyfile
        template: src=redis.conf dest=/etc/redis.conf
        tags: copyfile
        notify: restart redis
      - name: start
        service: name=redis state=started
      handlers:
      - name: restart redis
        service: name=redis state=restarted

    这样修改配置文件会自动触发重启redis

    参考资源 : http://blog.51cto.com/13525470/2112720 非常官方和到位

  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/amber-liu/p/10409554.html
Copyright © 2011-2022 走看看