zoukankan      html  css  js  c++  java
  • ansible管理windows主机

    服务端安装ansible

            sudo yum install python-pip

            sudo pip install pywinrm

            sudo pip install ansible

            yum安装的ansible无法调用pip安装的pywinrm插件,故而建议用pip安装ansible或者用源码包安装ansible

            使用sudo安装的应用在使用普通用户执行ansible命令的时候一定要修改安装文件的属主 否则会提示找不到对应的python模块

            使用sudo安装的话默认属主就是root用户 普通用户是没有权限访问的

            cd /usr/lib/python2.7/site-packages

            sudo chown -R jenkins:jenkins site-packages/

       企业windows一般都是AD域账户,AD域账号不支持basic权限,需要将ansible_winrm_transport登录选项设为ntlm

      192.168.30.137 ansible_ssh_user="Administrator" ansible_ssh_pass="123456"  ansible_ssh_port=5985 ansible_connection="winrm"       ansible_winrm_server_cert_validation=ignore

       192.168.30.137 ansible_ssh_user="Administrator" ansible_ssh_pass="123456"  ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport="ntlm"

        执行ansible命令的时候添加sudo

    windows端配置

        1.升级PowerShell版本到4.0
        2.Windows Server开启winrm服务【这个服务 远程管理作用】
        3.设置防火墙入站规则,允许5985端口入站通过

        

    管理windows主机任务

    pipeline {
       agent any
       environment {
       
        destPath="C:/PythonScriptTest"
       }
    
       stages {
       
          stage('GetCode'){
             steps {
                // Get some code from a GitHub repository
                git credentialsId: 'y44gitl4444ab', url: 'https://192.168.340.111:4444/wb/tsgz_zidonghua.git'
    
                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
             }
    
             post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                  print("getCode success")
                }
             }
          }
          
          stage("Deploy"){
               steps {
                  script {
                   print("Deploy success......")
                    
                     sh "sudo ansible windows -m win_copy -a 'src=/var/lib/jenkins/workspace/pipeline-test1/runall.py dest=${destPath}/runall.py'"
                  }
               }
               post {
                  success {
                    print("Deploy success......")
                  }
               }
          }
          
          stage("Start"){
               steps {
                  script {
                  
                    sh "sudo ansible windows -m win_shell -a 'python ${destPath}/runall.py'"
                  }
               }
               post {
                  success {
                    print("执行runall.py成功,本次流水线执行成功")
                  }
               }
          }
       }
    }
    流水线
    pipeline {
       agent any
       environment {
       
        destPath="C:/PythonScriptTest"
       }
    
       stages {
       
          stage('GetCode'){
             steps {
                // Get some code from a GitHub repository
                git credentialsId: 'yxhgitlab', url: 'https://192.168.30.111:8090/hfm/automation.git'
    
                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
             }
    
             post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                  print("getCode success")
                }
             }
          }
          
          stage("Deploy"){
               steps {
                  script {
                   print("Deploy success......")
                    
                     sh "sudo ansible windows -m win_copy -a 'src=/var/lib/jenkins/workspace/pipeline-test1/ dest=${destPath}/'"
                  }
               }
               post {
                  success {
                    print("Deploy success......")
                  }
               }
          }
          
          stage("Start"){
               steps {
                  script {
                    //sh "sudo ansible windows -m win_shell -a 'cd ${destPath}/Web'"
                    //sh "sudo ansible windows -m win_shell -a 'python runall.py'"
                    sh "sudo ansible windows -m win_command -a 'chdir=${destPath}/Web  python runall.py'"
                  }
               }
               post {
                  success {
                    print("执行runall.py成功,本次流水线执行成功")
                  }
               }
          }
       }
    }
    流水线2

     

      

        创建目录

             ansible 192.168.2.2 -m win_file -a 'path=D:\test state=directory'

        下发文件

            ansible 192.168.2.2 -m win_copy -a 'src=/etc/hosts dest=D:\hosts.txt'

        删除文件

            ansible 192.168.2.2 -m win_file -a 'dest=d:\config_dir\hosts.txt state=absent'

            ansible windows -m raw -a “cmd /c ‘move /y D:AnsibleproductDBFPlus.exe D:AnsibleackDBFPlus.exe’”

            ansible windows -m win_command -a “chdir=D:SupplierPay .http_restart.bat”

            ansible windows -m win_unzip -a"creates=no src=D:SupplierPay.zip dest=D:"

            ansible t-windows -m raw -a “taskkill /F /IM snmp.exe /T”

        删除目录

             ansible 192.168.2.2 -m win_file -a 'dest=d:\config_dir2 state=absent'

        执行cmd命令

             ansible 192.168.2.2 -m win_shell -a 'ipconfig'

        windows服务管理

             ansible 192.168.2.2 -m win_shell -a “net stop|start zabbix_agent” 

  • 相关阅读:
    多线程编程:阻塞、并发队列的使用总结
    为什么阿里的程序员那么帅?---原来他们都有"编码规约扫描"神器在手
    多线程编程:多线程并发制单的开发记录【一】
    如何使用线程锁来提高多线程并发效率
    如何在分布式环境中同步solr索引库和缓存信息
    前端性能优化指南
    DOM操作方法、属性
    CSS样式手册
    XSS跨站脚本攻击
    数组和对象的使用方法
  • 原文地址:https://www.cnblogs.com/yxh168/p/13437654.html
Copyright © 2011-2022 走看看