zoukankan      html  css  js  c++  java
  • 【原创】运维基础之Ansible(1)简介、安装和使用

    官方:https://www.ansible.com/

    一 简介

    Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

    It uses no agents and no additional custom security infrastructure, so it's easy to deploy - and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

    ansible的好处是快速、简单、易用、不需要安装agent,可以方便的用于配置管理和应用部署等自动化场景;

    二 安装

    1 安装

    yum install -y ansible

    2 测试

    # ansible --version

    ansible 2.7.5

      config file = /etc/ansible/ansible.cfg

      configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

      ansible python module location = /usr/lib/python2.7/site-packages/ansible

      executable location = /usr/bin/ansible

      python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

    3 配置目录结构

    # ls /etc/ansible/
    ansible.cfg hosts roles

    三 使用

    首先在/etc/ansible/hosts中根据部署结构将server划分到多个group,然后可以针对某个group执行某些操作;

    1 添加server分组配置

    vi /etc/ansible/hosts

    [all-servers]
    192.168.0.54
    192.168.0.55
    192.168.0.56

    以上配置了一个server_group名为all-servers,其中包含3台server;

    可以通过--list查看某个server_group下的所有server:

    # ansible all-servers --list

      hosts (3):

        192.168.0.54

        192.168.0.55

        192.168.0.56

    2 执行操作

    格式

    ansible $server_group -m $module_name -a $module_args

    例如ping:

    # ansible all-servers -m ping

    192.168.0.54 | SUCCESS => {

        "changed": false,

        "ping": "pong"

    }

    192.168.0.55 | SUCCESS => {

        "changed": false,

        "ping": "pong"

    }

    192.168.0.56 | SUCCESS => {

        "changed": false,

        "ping": "pong"

    }

    例如command:

    # ansible all-servers -m command -a "date"
    192.168.0.54 | CHANGED | rc=0 >>
    Sun Jan 13 17:14:06 CST 2019

    192.168.0.56 | CHANGED | rc=0 >>
    Sun Jan 13 17:14:06 CST 2019

    192.168.0.55 | CHANGED | rc=0 >>
    Sun Jan 13 17:14:06 CST 2019

    具体的module_name有哪些?可以通过命令查看

    # ansible-doc -l

    某个module_name的帮助也可以通过命令查看

    # ansible-doc $module_name

    比如:

    # ansible-doc shell

    > SHELL    (/usr/lib/python2.7/site-packages/ansible/modules/commands/shell.py)

            The `shell' module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the [command] module but runs the command

            through a shell (`/bin/sh') on the remote node. For Windows targets, use the [win_shell] module instead.

    # ansible-doc copy

    > COPY    (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)

            The `copy' module copies a file from the local or remote machine to a location on the remote machine. Use the [fetch] module to copy files from remote locations to

            the local box. If you need variable interpolation in copied files, use the [template] module. For Windows targets, use the [win_copy] module instead.

    常用module:ping,command,shell,copy,file,script,cron等...

    比如向某个group的所有server拷贝文件命令:

    # ansible all-servers -m copy -a 'src=/tmp/1.log dest=/tmp/'

    比如查看某个group的所有server的磁盘占用情况:

    # ansible all-servers -m shell -a "df -h"

    另外也可以直接指定ip来操作,比如:

    # ansible 192.168.0.55 -m ping

    其实如果只需要远程执行命令,ssh也可以

    ssh 192.168.0.55 "date" 

  • 相关阅读:
    Post返回json值
    调用接口并获取放回json值
    c# 获取IP
    sqlserver2008不允许保存更改
    判断客户端是否是手机或者PC
    3.docker tomcat集群
    1.docker 安装
    Maven profiles 多环境配置
    MySQL 定时任务
    MyBatis 三剑客
  • 原文地址:https://www.cnblogs.com/barneywill/p/10263278.html
Copyright © 2011-2022 走看看