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成功,本次流水線執行成功") } } } } }
建立目錄
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 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”