zoukankan      html  css  js  c++  java
  • ansible的简单使用

    环境搭建跳过(暂无,这个以后再学习学习,不要在意这些细节)

    首先,在环境搭建好后,用两台虚机来做测试,一台192.168.181.130做测试机,一台192.168.181.131为批量处理服务器

    编辑/home/ansible/app/ansible_config/hosts如下:(ip为需要远程管理的主机IP)

    [test]
    192.168.181.130 ansible_ssh_user=root ansible_ssh_pass=123456z
    

     ansible_ssh_user=root是130这台服务器的用户,密码对应的为ansible_ssh_pass

    这样将用户名和密码写清楚的做法,就可以处理130这台服务器,且不用通过免验证的方式

    # 检查主机连接
    # ansible caoguo -m ping

    [root@python2 ansible_config]# su - ansible
    [ansible@python2 ~]$ ansible test -m ping
    192.168.181.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
    }
    [ansible@python2 ~]$ exit
    logout
    [root@python2 ansible_config]#
    

      

    除此之外,hosts可以划多个部分,例如:

    [fenghuo]
    10.114.160.113 ansible_ssh_user=root ansible_ssh_pass=pass
    10.114.160.114 ansible_ssh_user=root ansible_ssh_pass=pass
    10.114.160.116 ansible_ssh_user=root ansible_ssh_pass=pass
    10.114.160.117 ansible_ssh_user=root ansible_ssh_pass=pass
    10.114.160.118 ansible_ssh_user=root ansible_ssh_pass=pass
    
    
    [test]
    10.114.160.112 ansible_ssh_user=root ansible_ssh_pass=pass
    

     有fenghuo和test两个组(友情建议,可以用ue与execl完美搭配)

    实际情况下(我用vm的虚拟机桥接到真实环境),需要用一台服务器来测试自己的脚本:

    测试test组(112这台服务器),一个拷贝文件到目标112服务器,一个是执行命令

    ansible test -m copy -a "src=/tmp/fenghuochange.sh dest=/tmp"
    ansible test -m shell -a 'sh /tmp/fenghuochange.sh'
    

      fenghuochange脚本:

    mv /etc/sysconfig/network-scripts/ifcfg-ens4f0 /etc/sysconfig/network-scripts/ifcfg-ens5f0
    sed -i 's/ens4f0/ens5f0/g' /etc/sysconfig/network-scripts/ifcfg-ens5f0
    sed -i 's/UUID/#UUID/g' /etc/sysconfig/network-scripts/ifcfg-ens5f0
    
    mv /etc/sysconfig/network-scripts/ifcfg-ens4f1 /etc/sysconfig/network-scripts/ifcfg-ens5f1
    sed -i 's/ens4f1/ens5f1/g' /etc/sysconfig/network-scripts/ifcfg-ens5f1
    sed -i 's/UUID/#UUID/g' /etc/sysconfig/network-scripts/ifcfg-ens5f1
    
    systemctl restart network
    

      #简单的改变网卡信息

    成功后:

    [ansible@python2 ~]$ ansible test -m copy -a "src=/tmp/fenghuochange.sh dest=/tmp"
    10.114.160.111 | SUCCESS => {
        "changed": true, 
        "checksum": "7ec006d69c14a0477bade27c6946a0b9051bab", 
        "dest": "/tmp/fenghuochange.sh", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "b09fa92337113022b6bd7106b87a84", 
        "mode": "0755", 
        "owner": "root", 
        "size": 489, 
        "src": "/root/.ansible/tmp/ansible-tmp-154065461.66-19901482505119/source", 
        "state": "file", 
        "uid": 0
    }
    [ansible@python2 ~]$ 
    [ansible@python2 ~]$ 
    [ansible@python2 ~]$ ansible test -m shell -a 'sh /tmp/fenghuochange.sh'
    10.114.160.111 | SUCCESS | rc=0 >>
    

      就可以批量处理fenghuo组了

    [ansible@python2 ~]$ ansible fenghuo -m copy -a "src=/tmp/fenghuochange.sh dest=/tmp"
    [ansible@python2 ~]$ ansible fenghuo -m shell -a 'sh /tmp/fenghuochange.sh'
    

      

  • 相关阅读:
    WP开发笔记——页面传参
    WP开发笔记——控件倾斜效果
    WP开发笔记——不同Item显示不同ApplicationBar:适用于Pivot与Panorama
    WP开发笔记——WP APP添加页面跳转动画
    WP开发笔记——去除 HTML 标签
    WP开发笔记——字符串 转 MD5 加密
    WP开发笔记——WP7 SDK使用技巧
    sphinx,coreseek安装
    yii2从零开始一,安装
    array_filter函数
  • 原文地址:https://www.cnblogs.com/lianyg/p/9863739.html
Copyright © 2011-2022 走看看