zoukankan      html  css  js  c++  java
  • ansible

    1:ansible的部署

    Ansible的安装部署及使用

    1:ansible的基本介绍

    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

       1.1:连接插件,connection plugins 负责和被监控实现通信

       1.2:host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

       1.3:各种模块核心模块, command模块 自定义模块;

       1.4:借助于插件完成记录日志邮件等功能

       1.5:playbook:剧本执行多个任务时,非必须可以然节点一次性运行多个任务。

    2:特性:

    2.1:不需要客户端,,基于模块进行安装,使用yaml编写playbook 基于SSH工作,

    3:准备工作:

    3.1:配置环境

    systemctl stop firewalld && systemctl disable firewalld

    setenforce 0 && sed -i 's/enforcing/disabled/g' /etc/selinux/config

    3.2 安装相关的依赖环境包

    yum install -y python36 python36-pip git

    /usr/bin/pip3.6  install virtualenv

    3.3:下载ansible并进行安装配置

    cd /opt/ && git clone -b stable-2.7 https://github.com/ansible/ansible.git

    /usr/bin/pip3.6 install paramiko PyYAML jinja2

    cd /opt/ansible/ && python3 setup.py install

    3.4 创建ansible配置文件,并开启ansible日志

    mkdir /opt/ansible/logs &&  cp /opt/ansible/examples/ansible.cfg  /opt/ansible/

    sed -i 's/#log_path/log_path/' /opt/ansible/logs/ansible.cfg

    ansible –version              #检查ansible的版本

    3.5生成公钥和私钥

    ssh-keygen -t rsa      #生成公钥和私钥

    ssh-copy-id -i ~/.ssh/id_rsa.pub  root@192.168.171.6

    vim /etc/ssh/ssh_config

    27     GSSAPIAuthentication yes

    vim  /opt/ansible/ansible.cfg

    14 inventory      = /opt/ansible/hosts    (这个地方根据需要自定义位置)

    71 host_key_checking = False

    188 command_warnings = False

    cp /opt/ansible/ansible.cfg  /opt/script/

    vim /opt/ansible/hosts

    [dns]

    192.168.171.6  

    4: 配置文件并执行ansible

      ansible all -m ping

    2:ansible的模块及命令

    command 模块

    例:ansible dns -m command -a 'free -m'

    ansible模块和ping 模块

    例:ansible-doc -l               #列出所有模块

            ansible-doc 模块名     #列出模块的帮助文档

    shell 模块

    例:ansible dns -m shell -a 'df -h'

    script模块

    例:先编写脚本

    vim disk.sh

    #!/bin/bash

    ansible ip -i host.conf -uroot -k -m shell -a 'df -hT|grep 'home';hostname'|tee > /tmp/1.txt
    ppovd_num=`grep "CHANGED" /tmp/1.txt|wc -l`
    failure_ip=`grep "UNREACHABLE" /tmp/1.txt|awk -F | '{print $1}'`
    if [ $ppovd_num -eq $total_ppovd_num ]; then
    sed -i 's# | CHANGED | rc=0 >>##g' /tmp/1.txt
    sed -ir 's#...../home$##g' /tmp/1.txt
    sed -ir 's/^.*xfs//g' /tmp/1.txt
    sed -ir 's#^.*ext4##g' /tmp/1.txt
    cat /tmp/1.txt| xargs -n 5|column -t|sort -k 5 > /tmp/2.txt
    paste /tmp/2.txt link_addr.txt > /tmp/print.txt
    sed -i '1i IP地址 磁盘总空间 已使用空间 剩余空间 主机名 后台地址' /tmp/print.txt
    column -t /tmp/print.txt
    else
    echo "磁盘获取成功$ppovd_num台少于$total_ppovd_num台ppovd,请重新执行"
    echo "磁盘获取未成功IP是: $failure_ip"
    fi

    ansible ip  -m script -a './opt/script/disk.sh'

    copy模块:复制文件到远程主机

    例:ansible all -m copy -a 'src=/etc/passwd dest=/tmp'     #复制文件下的passwd到所有主机的/tmp/目录下

    yum模块和service模块

    例:ansible all -m yum -a 'state=installed name=nginx'      #安装nginx

           ansible all -m service -a 'state=started name=nginx enable=yes'     #启动nginx 并设置开机自启

    lineinfile模块和replace模块

    例:ansible all -m linefile -a 'path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp="^IPADDR" line="IPADDR=192.168.171.7" '  整行替换

           ansible all -m replace -a 'path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp=".1.1" replace=".1.254" '   匹配指定的部分

    3:andible的playbook文件编写

     vim disk.yml

    ---

    - hosts: web

      remote_user: root

      tasks:

        - shell: uptime |awk '{printf("%.2f",$(NF-2))}'

          register: resutl

        - service:

            name:nginx

            state: stopped

          when: result.stdout|float >0.7

     ansible-playbook disk.yml

  • 相关阅读:
    震撼!一组你从未见过的惊艳照片(45图)
    看明白了这个故事不精神分裂算你厉害
    关于无法把程序(Adobe Fireworks CS5)添加到打开方式的解决办法
    打伞
    引用视频全屏播放代码
    居家生活实用生活小窍门集锦
    《西游记第一百零一回》第一百零一回观(转)
    保鲜膜的28种妙用!
    20155324 《信息安全系统设计基础》课程总结
    2017-2018-1 20155324 《信息安全系统设计基础》第十四周学习总结
  • 原文地址:https://www.cnblogs.com/will--1213/p/11828702.html
Copyright © 2011-2022 走看看