zoukankan      html  css  js  c++  java
  • Ansible的yaml文件


    ansible提供的脚本,遵循规范yaml(一般用于写配置文件)

    可用于配制文件的语言:yaml、xml、json
    
    - 冒号后面必须有空格
    - 横线后面必须要空格
    - 严格保持对齐
    - 等号前面不能有空格?
    
    yaml数据结构
    
    - 字典 key:value
    - 列表 (两种表示方式[], -)

    *** 基本命令**

    ansible-playbook -h
    ansible-playbook [options] playbook.yml [playbook2 ...]
    
    -C, --check # 白跑,执行但是不会有结果
    --list-hosts # 列出符合的主机
    -f FORKS, --forks=FORKS # 做并发
    --syntax-check # 检查语法
    -k, --ask-pass # 输入密码
    
    操作示例:
    
    - - hosts: web
    remote_user: root
    tasks:
    - name: createuser
    user: name=jason666 home=/opt/jason666 uid=4000
    
    ansible-playbook --syntax-check p1.yml # 检测语法
    ansible-playbook -C p1.yml # 干跑 
    ansible-palybook p1.yml # 真正执行文件

    *** 传参**

    - hosts: web
      tasks:
      - name: createuser
        user: name=jason33
    # 创建用户jason33 过段时间又需要创建其他用户tank33  频繁修改文件yml明显不合理
    
    # 解决方案  传参!
    # 方式1:命令中传值
    - hosts: web
      tasks:
      - name: create{{user}}
        user: name={{user}}
    ansible-playbook -e user=jason20 p3.yml
    
    # 方式2:hosts文件中主机后面直接添加
    [web]
    192.168.226.[101:102] user=jason30
    
    # 方式3:hosts文件中新增
    [web:vars]
    user=jason31
    
    # 方式4:yml文件中配置
    - hosts: web
      vars:
      - user: jason32
      tasks:
      - name: create{{user}}
        user: name={{user}}
    
    # 方式5:了解
    - hosts: web
      tasks:
      - name: yum
        yum: name=bc
      - name: sum
        shell: echo 11+22|bc
        register: user
      - name: echo
        shell: echo {{user.stdout}} > /tmp/echo.txt 
      - name: create{{user.stdout}}
        user: name=alex{{user.stdout}}
        
    传参优先级:-e > playbook > hosts

    **tags可以单独调用任务**

    - hosts: web
    tasks:
    - name: install
    yum: name=redis
    - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    - name: start
    service: name=redis state=started
    
    yum install -y redis # 安装redis
    ansible-playbook --syntax-check p7.yml # 检测语法
    ansible-playbook -t copy p7.yml # 执行

    **handlers**

    - hosts: web
    tasks:
    - name: install
    yum: name=redis
    - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart # 触发handlers里面的任务
    - name: start
    service: name=redis state=started
    handlers:
    - name: restart
    service: name=redis state=restarted




    • **template**

    绝对路径

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

    相对路径

    - hosts: web
      tasks:
      - name: install
        yum: name=redis
      - name: copyfile
        template: dest=/etc/redis.conf src=redis.conf.j2
        tags: copy
        notify: restart
      - name: start
        service: name=redis state=started
      handlers:
      - name: restart
        service: name=redis state=restarted
    # 在当前目录下创建一个templates的目录,就可以使用相对路径

    **when 类似于后端if判断**

    - hosts: web
      tasks:
      - name: copyfile
        copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
        when: ansible_distribution_major_version=="7"
      - name: copyfile
        copy: content="小弦切切如私语" dest=/tmp/a.txt
        when: ansible_distribution_major_version=="6"
          
          
    - hosts: web
      tasks:
      - name: copyfile
        copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
        when: user=="4"
      - name: copyfile
        copy: content="小弦切切如私语" dest=/tmp/a.txt
        when: user=="3"
    • with_items

    - hosts: web
      tasks:
      - name: createuser
        user: name={{item}}
        with_items:
        - jason50
        - tank50
        - oscar50
    
    - hosts: web
      tasks:
      - name: createuser
        user: name={{item}}
        with_items:
        - jason51
        - tank51
        - oscar51
      - name: creategroup
        group: name={{item}}
        with_items:
        - jason60
        - tank60
        - oscar60

    **循环嵌套**

    - hosts: web
    tasks:
    - name: crateuser
    user: name={{item.name}} group={{item.group}}
    with_items:
    - {"name":jason52,"group":jason60}
    - {"name":tank52,"group":tank60}
    - {"name":oscar52,"group":oscar60}
    
    demo:安装nginx并启动,设置开机自启动,指定监听地址为ip地址
    - hosts: web
    tasks:
    - name: install
    yum: name=nginx
    - name: copyfile
    template: dest=/etc/nginx/nginx.conf src=/etc/nginx/nginx.conf
    - name: start
    service: name=nginx state=started enabled=yes

    roles

    • 目录结构清晰

    • 可以相互调用 - import_tasks: roles/nginx/tasks/install.yml

    • 备份方便

    # 在任意位置新建roles文件夹  文件夹内建不同的功能
    mkdir /data
    cd /data/
    mkdir roles
    cd roles/
    mkdir {nginx,uwsgi,redis,mysql}
    cd nginx
    # 之后不需要在文件内按照hosts、tasks、handlers等顺序依次书写  而是以文件夹的形式创建
    data/roles/nginx/
    ├── files       # -- 静态文件
    │   └── c.txt
    ├── handlers    # -- 触发的任务
    │   └── main.yml   
                    # - name: restart
            #   service: name=nginx state=restarted
    ├── tasks       # -- 任务(必须的)
    │   ├── copyfile.yml  # 只需要书写对应的yml格式任务即可
                # - name: copyfile
              # template: dest=/etc/nginx/nginx.conf src=/etc/nginx/nginx.conf    
    │   ├── install.yml
                # - name: install
            # yum: name=nginx
    │   ├── main.yml  # (必须有main.yml文件  将其他yml文件导入即可也可以这里面直接写)
                # - import_tasks: install.yml
          # - import_tasks: copyfile.yml
          # - import_tasks: start.yml
          # - name: file
          #   copy: dest=/tmp/aaa.txt src=c.txt
          # - name: createuser
          #   user: name={{ user }}
          #   notify: restart
    │   └── start.yml
                # - name: start
          # service: name=nginx state=started enabled=yes
    ├── templates  # -- 动态文件,需要传递参数  拷贝nginx配置文件cp /etc/nginx/nginx.conf . 
    │   └── nginx.conf  # yum install -y nginx下载并将配置文件弄一弄
    └── vars      # -- 变量
        └── main.yml
                  # {"user":jason70}
    
    data/nginx.ym
        # - hosts: web
      #   roles:
      #   - nginx

    # nginx中worker_connections默认1024,理论最大在100万左右,再多nginx就无法正常启动了
    # nginx中listen监听ipv4和ipv6,default_server含义:nginx默认多个server,访问使谁写了default_server就返回谁
    # 上面的代码如果演示不成功 将nginx配置文件中的default_server删除即可**

    查找顺序

    - 主文件看到roles,就会去roles目录下面找对应的目录
    - 先去tasks目录里面找main.yml入口文件,如果遇到import_task则加载任务
    - 如果遇到了template,则去templates目录里面找文件
    - 如果遇到了copy,则去files目录里面找文件
    - 如果遇到了变量,则去vars目录里面找main.yml文件
    - 如果遇到了notify,则去handlers目录里面找main.yml文件

    ansible-galaxy

    # roles文件参考网站:https://galaxy.ansible.com/
    
    # 下载nginx相关roles文件
    ansible-galaxy install geerlingguy.nginx
  • 相关阅读:
    hash介绍
    序列化
    面向对象编程
    计算机系统基础知识05
    19、Python之队列
    18、Python之多线程
    17、Python之paramikomo
    16、Python之socket网络编程
    15、Python之异常处理
    14、Python之反射
  • 原文地址:https://www.cnblogs.com/yangmeichong/p/11909855.html
Copyright © 2011-2022 走看看