服務端安裝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成功,本次流水線執行成功") } } } } }
創建目錄
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:\Ansible\product\DBFPlus.exe D:\Ansible\back\DBFPlus.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”