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” 

原文地址:https://www.cnblogs.com/yxh168/p/13437654.html