• ansible复习笔记_playbook-从零到无


    --创建时间:2021年3月9日

    --修改时间:2021年3月9日

    --作者:飞翔的小胖猪

    yaml语法格式

    • 每单一文件第一行,使用 "---"开始。在结尾的时候使用三个点“...”来表示,不是必须的。
    • 次行开始写playbook的内容。
    • 使用#表示注释代码。
    • 缩进必须是统一的,不能出现“tab”和“空格”混用。
    • 同一级别的缩进一致。
    • yaml文本对大小写敏感。
    • 多个k/v可同行写也可单独成行写,在一行中书写多个K/V时每个键值对使用“,”隔开。
    • K/V中的v可以是单个字符串亦可时另一个列表。
    #基础playbook格式     
    ---
    - hosts: 应用主机
    gather_facts:no 设置不收集被控主机信息 tasks: - name: 自定义task名 模块名: 模块相关参数 - name: 自定义task名 模块名: 模块相关参数 ...

    handlers/notify

    handlers本质上是tasks list,与notify成对出现。类似于触发器,当playbook剧本中的某个task满足条件后会通过notify调用对应的handlers任务。

    ---
    - hosts: test8
      remote_user: root
      gather_facts: no
      vars:
      - host: lvant
    
      tasks:
      - name: copy config to remote hosts
        copy: src=/tmp/test1   dest=/tmp/3333
        notify: touch file
    
      handlers:
      - name: touch file
        file: path=/tmp/{{testvar}}  state=touch

    主机定义

    playbook文件定义在那些主机上生效需要关键参数是hosts,该参数的主机列表来源于/etc/ansible/hosts(默认情况下)定义的主机清单。

    host1                    #当台主机,限定作用于某一个主机。
    host1:host2              #某两台主机,使用 : 号隔开表示或者。
    group1:group2            #或者,两个组的并集
    group1:&group2           #与,两个组的交集
    group1:!group2           #在group1组,但不在group2组

    变量

    变量定义:   K=V ,如variable=values

    变量调用: {{ variable }}   或  “{{ variable }}”

    变量定义方式:

    •  命令行中定义: ansible-playbook -e k=V 
    • yaml文件中定义:
      ---
      - hosts: all
        vars:
         - host: lvant
         - system_os: centos
        tasks:
         - name: file create
           file: path=/root/{{ host }}_{{ system_os }}.file state=touch
    • 使用变量文件:
      # cat vars.yml      //变量文件
      ---
      system_version: 7
      child_version: 6


      # cat test_file_var.yml //playbook文件
      ---
      - hosts: all
        vars_files:
         - vars.yml
        tasks:
         - name: touch file
           file: path=/root/{{ system_version }}_{{ child_version }}.filetxt state=touch
    • /etc/ansible/hosts中定义:
      #其中host和machine就是自定义的变量
      [lvs] lvs1 host=test1 ansible_ssh_host=192.168.111.11 ansible_ssh_user='root' ansible_ssh_pass='yinwan' ansible_ssh_port=22 lvs2 host=test2 ansible_ssh_host=192.168.111.12 ansible_ssh_user='root' ansible_ssh_pass='yinwan' ansible_ssh_port=22 [lvs:vars] machine=lvs

    条件语句

    when写在tasks下模块下,表示满足对应条件时执行相关操作。

    ---
    - hosts: websrvs
      remote_user: root
      tasks:
        - name: "shutdown RedHat flavored systems"
          shell: echo "`date` test_when ." >> /var/log/message
          when: ansible_os_family == "RedHat"

    迭代操作

    迭代操作的实质是在tasks中的模块中设置固定变量{{ item }},然后通过依次调用模块下with_items中定义的值。

    [root@135 7_with_items]# cat test_item.yaml 
    ---
    - hosts: all
      gather_facts: no
      tasks:
      - name: echo info
        shell: echo "`date` {{ item  }}"
        with_items:
        - haha
        - bebe
        - test11
      - name: user create
        user: name=lvanwww  system=yes

    迭代嵌套

    ---
    - hosts: all
      gather_facts: no
      tasks:
      - name: create group
        group: name={{ item }} state=present
        with_items:
        - test222
        - test333
        - test444 
      - name: create user
        user: name={{ item.name }} group={{ item.group }} state=present
        with_items:
        - { name: 'test22', group: 'test222' }
        - { name: 'test33', group: 'test333' }
        - { name: 'test44', group: 'test444' }
  • 相关阅读:
    用grunt搭建自动化的web前端开发环境-完整教程
    SQL Server:触发器详解
    利用junit对springMVC的Controller进行测试
    jquery-barcode:js实现的条码打印
    16个良好的 Bootstrap Angularjs 管理后台主题
    Spring Security 4 Hello World Annotation+XML
    intellij 13新建javaweb项目并用tomcat 7启动
    JavaScript类和继承:constructor属性
    javascript 的面相对象
    javascript call apply bind caller callee 的用法
  • 原文地址:https://www.cnblogs.com/Pigs-Will-Fly/p/14505693.html
Copyright © 2020-2023  润新知