zoukankan      html  css  js  c++  java
  • (三)ansible playbook

    一,YAML语法

    1. YAML的语法和其他高阶语言类似并且可以简单表达清单、散列表、标量等数据结构。(列表用横杆表示,键值对用冒号分割,键值对里又可以嵌套另外的键值对)
    2. YAML文件扩展名通常为.yaml或者.yml
    3. 一定要对齐,只能使用空格
    name: tom
    age: 21
    gender: male
    spourse:
        name: lily
        gender: female
    children:
        - name: susan
          age: 2
          gender: feamle
        - name: sunny
          age: 10
          gender: male

     

    二,ansible playbook 核心组件

    • tasks:任务
      • name
      • notify 调用handlers
      • 模块名称(shell,yum,script)
    • variables:变量
    • templates:模板
    • handlers:处理器
    • roles:角色
    • host :执行对象
    • remote_user:定义执行命令的远程用户

    三,示例

    示例一

    #vim /root/first.yml
    
    - hosts: all
      remote_user: root
      vars: httpd_port=80
    
      tasks:
      - name: install httpd
        yum: name=httpd state=present
      - name: install php
        yum: name=php state=present
      - name: start httpd
        service: name=httpd state=started enabled=true

     

    hosts 定义单个主机或组
    remote_user 定义执行命令的远程用户
    vars 定义变量,
    tasks 定义执行哪些命令,
    handlers 定义调用哪些处理器

    vars(变量):

    变量命名: 字母数字下划线组成,只能以字母开头

    变量种类:

    facts(内置变量)
    由远程主机发回的主机属性信息,这些信息被保存在ansible变量当中
    例如:ansible 192.168.238.170 -m setup 来获取远程主机上的属性信息,这些属性信息保存在facts中
     
    通过命令行传递
    通过命令行传递:ansible-playbook test.yml –extra-vars “host=www user=tom“(如果剧本中已有此处定义的变量则会被覆盖)
     
    通过roles传递
    主机变量
    在/etc/ansible/hosts中定义
    [web1]
    192.168.1.1 name=haha
     
    组变量
    [group_name:vars]
    foo=bar

    示例二

    #vim /root/second.yml
    - hosts: web1
      remote_user: root
      vars:
        username: bob
        password: 123
    
      tasks:
      - name: add user
        user: name={{ username }} state=present   #
        when: ansible_os_family == "Debian"
      - name: set password
        shell: echo {{ password }} |passwd --stdin {{ username }}
      - name: install httpd php
        yum: name={{ item }} state=present
        with_items:
          - httpd
          - php
      - name: add two users
        user: name={{ item }} state=present groups={{ item.groups }}
        with_items:
        - { name: 'user1', groups: 'group1'}
        - { name: 'user2', groups: 'group2'}

     

    在playbook中调用变量的方式为{{ variable }}
    when 语句用来条件测试
    ansible_os_family 是facts中内置的属性信息 ansible_os_family的信息可以使用ansible all -m setup | grep ansible_os_family 查看
    在task中调用内置的item变量;在某task后面使用with_items语句来定义元素列表
     

    示例三

    #vim /root/third.yml
    
    - hosts: web1
      remote_user: root
      vars: 
        httpd_port=80
    
      tasks:
      - name: install httpd
        yum: name=httpd state=present
      - name: install php
        yum: name=php state=present
      - name: copy config file
        copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
        notify: restart httpd
      - name: start httpd
        service: name=httpd state=started enabled=true
    
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
    copy中复制过去的文件跟远程主机上的文件不同,就通过notify调用handlers,即重启httpd服务
    handler是重启服务是最通用的用法
     

    示例四

    #/root/httpd.conf 
    Listen {{ http_port }}
    #vim /root/fourth.yml
    
    - hosts: web1
      remote_user: root
      vars: 
        http_port: 80
    
      tasks:
      - name: install httpd
        yum: name=httpd state=present
      - name: copy config file
        template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf  #变量 http_port 将在/etc/httpd/conf/httpd.conf中生效
        notify: restart httpd
      - name: start httpd
        service: name=httpd state=started enabled=true
    
      handlers:
      - name: restart httpd
        service: name=httpd state=restarted
    templates:用于生成文本文件(配置文件)
    模板文件中可使用jinja2表达式,表达式要定义在{{ }},也可以简单地仅执行变量替换
     

    示例五

    roles:roles用于实现“代码复用
    roles以特定的层次型格式组织起来的playbook元素(variables, tasks, templates,handlers),可被playbook以role的名字直接进行调用
     

    roles的文件结构:

    files/:此角色中用到的所有文件均放置于此目录中
    templates/: Jinja2模板文件存放位置
    tasks/:任务列表文件;可以有多个,但至少有一个叫做main.yml的文件
    handlers/:处理器列表文件;可以有多个,但至少有一个叫做main.yml的文件
    vars/:变量字典文件;可以有多个,但至少有一个叫做main.yml的文件
    meta/:此角色的特殊设定及依赖关系
     
    ansible服务器上执行
    mkdir -p /opt/ansible/roles
    cd /opt/ansible/roles
    mkdir -p web/{files,templayes,tasks,handlers,vars,meta}
    1,定义变量文件(web/vars/main.yml)
    user: tom
    group: tom
    http_port: 8080
    2,定义任务文件(web/tasks/main.yml)
    - name: install httpd
      yum: name=httpd state=present
    - name: copy config file
      template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
      tags: conf
    - name: start httpd
      service: name=httpd state=started enabled=true
    #这里的template指的是相对路径-->web1/templates
    #tags可以在运行时指定标签任务
    3,定义handlers文件(web/handlers/main.yml)
    handlers:
    - name: restart httpd
      service: name=httpd state=restarted
    4,定义配置文件(web/templates/httpd.conf)
    ……
    Listen {{ http_port }}
    ……
    5,定义一个调用roles文件(/opt/ansible/web_install.yml)
    - hosts: web1
      remote_user: root
      roles:
        - web1
        #- { role:web2, http_port:8080 }
    #hosts:web1 指在/etc/ansible/hosts中定义的组,上面有定义
    #roles: web1 指的是当前目录下的web1目录,也可通过role传递变量, 也可调用多个role
    #这样只需更改hosts的主机就可以实现不同主机的代码重用了

    四,运行playbook

    ansible-playbook web1.yml
    指定运行任务:
    ansible-playbook -t conf web1.yml

     

     

     

     

  • 相关阅读:
    centos 用户管理
    rsync 实验
    文件共享和传输
    PAT 1109 Group Photo
    PAT 1108 Finding Average
    PAT 1107 Social Clusters
    PAT 1106 Lowest Price in Supply Chain
    PAT 1105 Spiral Matrix
    PAT 1104 Sum of Number Segments
    PAT 1103 Integer Factorization
  • 原文地址:https://www.cnblogs.com/xiao2er/p/10240031.html
Copyright © 2011-2022 走看看