ansible 基本用法

Ansible 是一个开源的IT自动化工具,专注于配置管理、应用部署、任务执行和编排。它由Michael DeHaan于2012年创建,并由Red Hat公司维护和发展。Ansible使用YAML语言编写剧本(Playbooks),这些剧本描述了管理和配置远程系统所需的步骤序列。

playbook  是用 yaml的文件形式写的

    yaml 是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。Clark Evans在2001年首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。当前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。 

  YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。

yaml  的写法参考 https://yaml.org

以下是我工作中的一个示例,主要功能是监控系统服务是否正常

调用的外部文件 machine_info



---
  - name: mem_info
    shell: free -g
    register: mem_result
  - name: print mem_result
    debug: var=mem_result verbosity=0 #输出系统信息

  - name: disk_info
    shell: df -h
    register: disk_result
  - name: print disk_result
    debug: var=disk_result verbosity=0

  - name: run_time
    shell: uptime | cut -d ' ' -f 3-
    register: run_time
  - name: show_run_time
    debug: var=run_time verbosity=0

主配置文件 hosts

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=123254
ansible_become_pass=123254
ansible_python_interpreter=/usr/bin/python

[all:children]
prepro

[adapter]
test001

[disp]
test002

[video]
test[003:005]
test129

[prepro]
test[006:020]

[kwd_ww]
test[021:114]
test[126:128]
test[133:134]


[kwd_hh]
test[116:120]
test[124:125]

[kafka]
test[135:139]

[web]
test140

[kwd_bak]
test[125:128]

[logstash]
test141

[sch]
test[147:150]

 主体部分 main

---
  - name: print adapter info
    hosts: adapter
    gather_facts: no  #不收集设备信息
    remote_user: test
    serial: 10  #并发数


    tasks:
      - include: mechine_info.yaml #引用外部文件

      - name: adapter_service_status
        shell: systemctl status {{ item }}

        with_items:
          - "adapter-offline.service"
          - "adapter-opt.service"
          - "adapter-vid.service"
          - "adapter-test-101.service"
          - "adapter-test-102.service"
        register: adapter_return_value
      - debug:
          var: adapter_return_value

    tags:
      - adapter

  - name: print disp info
    hosts: disp
    gather_facts: no
    remote_user: test

    tasks:
      - include: mechine_info.yaml

      - name: dispatcher_service_status
        shell: systemctl status {{ item }}

        with_items:
        - "disp-ww.service"
        - "disp-hh.service"
        - "disp-vv.service"
        - "disp-vp.service"
        register: disp_return_value
      - debug:
          var: disp_return_value

    tags:
      - dispatcher


  - name: print vp info
    hosts: vp
    gather_facts: F
    remote_user: test
 
    tasks:
      - include: mechine_info.yaml
    
      - name: vp_service status
        shell: systemctl status {{ item }}
        
        with_items:
        - "vp.service"
        - "fileserver.service"
        register: vp_return_value
      - debug:
          var: vp_return_value

    tags:
      - vp
          
  - name: print ww info
    hosts: kwd_ww
    gather_facts: F
    remote_user: test
    tasks:
      - include: mechine_info.yaml

      - name: ww_service status
        shell: systemctl status {{ item }}

        with_items:
        - "kwd-ww.service"
        register: ww_return 
      - debug:
          var: ww_return

    tags:
      - ww

  - name: "print hh info"
    hosts: kwd_hh
    gather_facts: F
    remote_user: test
    
    tasks:
      - include: mechine_info.yaml

      - name: hh_service status
        shell: systemctl status {{ item }}

        with_items:
        - "kwd-hh.service"
        register: hh_return 
      - debug:
          var: hh_return

    tags:
      - hh

  - name: print kafka info
    hosts: kafka
    gather_facts: F
    remote_user: test
    tasks:
      - include: mechine_info.yaml

      - name: service status
        shell: "{{ item }}"

        with_items:
        - "ps -ef | grep kafka"
        - "ps -ef | grep zookeeper"
        register: kafka_return
      - debug:
          var: kafka_return
        
    tags:
      - kafka

  - name: print logstash info
    hosts: logstash
    gather_facts: F
    remote_user: test
    tasks:
      - include: mechine_info.yaml

      - name: service status
        shell: "{{ item }}"

        with_items:
        - "systemctl status logstash-in.service"
        - "systemctl status logstash-all.service"
        - "systemctl status logstash-stat.service"
        - "systemctl status logstash-vv.service"
        - "ps -ef | grep elasticsearch"
        - "ps -ef | grep hadoop"
        register: logstash_return
      - debug:
          var: logstash_return
        
    tags:
      - logstash

  - name: print sch info
    hosts: sch
    gather_facts: F
    remote_user: test
    tasks:
      - include: mechine_info.yaml

      - name: service_status
        shell: "{{ item }}"

        with_items:
        - "systemctl status objpack-kwd.service"
        - "systemctl status objsch-hh.service"
        - "systemctl status objsch-ww.service"
        - "ps -ef | grep elasticsearch"
        register: sch_return
      - debug:
          var: sch_return

    tags:
      - sch

  - name: vv info
    hosts: vv
    gather_facts: T
    remote_user: test
    tasks:
      - include: mechine_info.yaml

      - name: service test003
        shell: "{{ item }}"
        when: (ansible_nodename == "test003" or ansible_nodename == "test004")

        with_items:
        - "systemctl status vp-vv.service"
        - "systemctl status kwd-ww-vv.service"
        - "systemctl status node_exporter.service"
        - "ps -ef | grep keti_webserver.py"
        register: test003_return
      - debug:
          var: test003_return

      - name: service test005
        shell: "{{ item }}"
        when:
          - ansible_nodename == "test005"

        with_items:
        - "systemctl status kwd-ww-vv.service"
        - "systemctl status node_exporter.service"
        - "ps -ef | grep keti_webserver.py"
        register: test005_return
      - debug:
          var: test005_return

      - name: service test129
        shell: "{{ item }}"
        when:
          - ansible_nodename == "test129"

        with_items:
        - "systemctl status kwd-hh-vv.service"
        - "systemctl status node_exporter.service"
        register: vv_return
      - debug:
          var: vv_return

    tags:
      - vv

你可能感兴趣的:(ansible,运维)