zoukankan      html  css  js  c++  java
  • 自动化运维 Ansible

    自动化运维 Ansible

    特性

    (1)、no agents:不需要在被管控主机上安装任何客户端;

    (2)、no server:无服务器端,使用时直接运行命令即可;

    (3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

    (4)、yaml,not code:使用yaml语言定制剧本playbook;

    (5)、ssh by default:基于SSH工作;

    (6)、strong multi-tier solution:可实现多级指挥。

    1、运行 easy_install 安装

    easy_install simplejson

    easy_install pip

    yum install gcc python-devel

    easy_install ansible

    pip list

    2、Ansible配置

    (1)、SSH免密钥登录设置

    生成公钥/私钥

    #ssh-keygen -t rsa -P ''

    (2)、将生成的 密钥(/root/.ssh/id_rsa.pub) 发布到 被控主机 上

    客户机上: 

    cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys

    chmod 600 /root/.ssh/authorized_keys

    完成以后记得删除 密钥 ! ( rm -rf /tmp/id_rsa.pub )

    (3)、ansible配置 (配置文件目录为 /etc/ansible )

    修改配置文件,指定 ssh 密钥:

    mkdir -p /etc/ansible

    #vim /etc/ansible/ansible.cfg

    [defaults]

    remote_port = 22                          #被控端 SSH 端口 默认为22

    private_key_file = /root/.ssh/id_rsa      #密钥 需指定 id_rsa 这个密钥

    hostfile      = /etc/ansible/hosts

    配置被控主机(主机组定义)IP

    #vim /etc/ansible/hosts

    [yunwei]

    172.24.0.14

    172.24.0.15

    (3)、简单测试

    ansible yunwei -m command -a "uptime"                      

    yunwei = 指定被控端的组  

    -m command = 运行命令 

    -a 'uptime' = 命令

    3、常用模块使用

    (1)、setup

    ## 用来查看远程主机的一些基本信息

    #ansible yunwei -m setup

    (2)、ping

    ## 用来测试远程主机的运行状态

    #ansible yunwei -m ping

    (3)、shell

    # 先在本地创建一个SHELL脚本

    vi /tmp/yunwei.sh

    ------------------------------------------------------------------------------------

    #!/bin/sh

    ifconfig |grep inet -m 1|grep -v '127.0.0.1' |cut -d: -f2|awk '{print $2}'

    ------------------------------------------------------------------------------------

    保存 并授权

    chmod +x yunwei.sh 

    # 将创建的脚本文件分发到远程  ( src = 本地脚本目录   dest = 被控机目录  owner = 权限 )

    ansible yunwei -m copy -a "src=/tmp/yunwei.sh dest=/tmp/yunwei.sh owner=root group=root mode=0755"

    # 远程执行

    ansible yunwei -m shell -a "/tmp/yunwei.sh"

    (4)、 yum           (安装ntpdate时间同步)

    ansible yunwei -m yum -a "name=ntpdate state=installed"

    同步时间

    ansible yunwei -m command -a "ntpdate time.windows.com"

  • 相关阅读:
    QT 读写sqllite数据库
    SQLLite 简介
    arcengine 开发经典帖 【强烈推荐仔细研读】
    IHookHelper的用法
    ArcSDE中Compress与Compact的区别
    以Network Dataset(网络数据集)方式实现的最短路径分析
    ArcGIS网络概述
    ClassLoader.getResourceAsStream(name);获取配置文件的方法
    Xml中SelectSingleNode方法,xpath查找某节点用法
    Spring整合JUnit4测试使用注解引入多个配置文件
  • 原文地址:https://www.cnblogs.com/jicki/p/5546994.html
Copyright © 2011-2022 走看看