日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Ansible在windows的使用

發布時間:2023/12/1 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 Ansible在windows的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Ansible在windows的使用

1.linux離線安裝ansible

參考博客: ansible離線安裝

相關的安裝包:

鏈接:https://pan.baidu.com/s/1vqUSk3B_XKLAvzFGAr0HIw
提取碼:3844

使用相關的命令進行安裝:

# 安裝包解壓
tar -xzvf ansible-2.4-rpms.el7.tar.gz
# 進入安裝包
cd ansible-2.4-rpms.el7
# 安裝ansible依賴內容
rpm -ivh PyYAML*rpm libyaml*rpm python-babel*rpm python-backports*rpm python-backports-ssl_match_hostname*rpm python-cffi*rpm python-enum34*rpm python-httplib2*rpm python-idna*rpm python-ipaddress*rpm python-jinja2*rpm python-markupsafe*rpm python-paramiko*rpm python-passlib*rpm python-ply*rpm python-pycparser*rpm python-setuptools*rpm python-six*rpm python2-cryptography*rpm python2-jmespath*rpm python2-pyasn1*rpm sshpass*rpm --nodeps --force
# 安裝ansible
rpm -ivh ansible-2.4.2.0-2.el7.noarch.rpm

安裝完成后查看ansible的版本信息:

另外一種ansible的安裝方式時使用 包裝好 一個yum源進行安裝,未嘗試。

2.Linux安裝第三方庫

當前的python環境版本為2.7.5

下載第三方庫,網址為: https://pypi.org/

離線安裝的第三方庫有:

pywinrm-0.4.2

ntlm-auth-1.1.0.tar.gz

requests_ntlm-1.1.0.tar.gz

xmltodict-0.12.0-py2.py3-none-any.whl

相關的版本信息如下:

3.配置windows系統

1.改powerShell的策略為remotesigned,否則運行不了powerShell腳本文件。

PS C:\Users\huangp> get-executionpolicy
Restricted
PS C:\Users\huangp> set-executionpolicy remotesigned
PS C:\Users\huangp> get-executionpolicy
RemoteSigned

2.如果Powshell版本不對,執行如下命令更新powshell的版本

目前我是用的是 powershell版本為 5.1.14393

# 查看windows的PowerShell的版本的方法
PS C:\Users\ywwei> host

所以我的版本不需要更新。

下面是更新版本信息命令。

#更新PowerShell 2.0到3.0的腳本:
#https://github.com/ansible/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1
#調用的命令可以如下
PS C:\Users\huangp> & "E:/temp/upgrade_ps3.ps1"
#或者
PS C:\> powershell.exe -ExecutionPolicy RemoteSigned   -file "C:\p test.ps1"

3.配置遠程控制

下載并運行https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

如果嫌棄Github下載速度太慢:我上傳了到了自己的阿里云盤 鏈接:https://www.aliyundrive.com/s/opYkwmSnQk5

# 進入我我自己的桌面
PS C:\Users\ywwei> cd C:\Users\ywwei\Desktop
# 執行下載后的powershell腳本
PS C:\Users\ywwei\Desktop> .\ConfigureRemotingForAnsible.ps1

4.在PowerShell中執行:

# 啟動winrm
winrm qc
# 設置相關的配置
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

5.查看winrm配置信息

# 查看winrm配置信息
winrm get winrm/config

4.配置Linux主機

4.1配置Linux主機清單

修改主機清單內容

# 添加以下內容
$ sudo vim /etc/ansible/hosts

Linux的主機清單如下所示:

# 個人電腦
[windows]
# ywwei的電腦
192.168.56.101 ansible_ssh_user="LG\ywwei" ansible_ssh_pass="Hitrend@0701" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm# 大表架
[largeWatchStand]
# 表架6
192.168.41.29  ansible_ssh_user="admin" ansible_ssh_pass="dddddddd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm
# 表架7
192.168.41.47  ansible_ssh_user="admin" ansible_ssh_pass="dddddddd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm# 點對點表架
[PointToPointMeterRack]
# 包含的測試用例有	浙江、山東、黑龍江、南網深化應用、QMD
192.168.91.62  ansible_ssh_user="user" ansible_ssh_pass="dddddddd" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm

查看主機清單內容

# 查看主機清單
$ sudo cat /etc/ansible/hosts

檢查是否能夠連通

# 查看是否ping通
$ ansible all -m win_ping

如果不能夠連通,請檢查,或者請查看我的踩坑記錄是否有踩過坑。

4.2執行Ansible腳本

完成準備后,可以寫個小例子進行測試驗證

執行測試腳本

# 測試windows組是否連同
ansible windows -m win_ping
# 查看所有主機是否都可以連通
ansible all -m win_ping

連接成功:

4.3使用常用模塊

4.3.1創建文件夾

ansible windows -m win_file -a 'dest=C:/test state=directory'

4.3.2遠程執行cmd 命令

# 遠程執行cmd 命令
ansible windows  -m win_shell -a 'ipconfig'

目前我的桌面上存在兩個文件夾,A文件夾下面存放了 demo.txt 文件:

# 復制文件
ansible windows  -m win_shell -a 'copy D:\Users\ywwei\Desktop\A\demo.txt  D:\Users\ywwei\Desktop\B'
# 移動文件
ansible windows  -m win_shell -a 'move D:\Users\ywwei\Desktop\A\demo.txt  D:\Users\ywwei\Desktop\B'
# 修改文件名稱
ansible windows  -m win_shell -a 'ren  D:\Users\ywwei\Desktop\A\demo.txt demo1.txt'
# 刪除文件
ansible windows  -m win_file -a 'path=D:\Users\ywwei\Desktop\A\demo2.txt state=absent'
# 修改文件名稱
ansible windows  -m win_shell -a 'ren  D:\Users\ywwei\Desktop\A\demo.txt demo1.txt'# 使用cmd cllone代碼
# clone command
git clone http://192.168.91.237:8929/test/python.git D:\Users\ywwei\Desktop\python
ansible windows  -m win_shell -a 'git clone http://192.168.91.237:8929/test/python.git D:\Users\ywwei\Desktop\python'

4.3.3文件目錄操作

# 刪除文件
ansible windows  -m win_file -a 'dest=c:\test\hosts.txt state=absent'
# 刪除目錄
ansible windows  -m win_file -a 'dest=c:\test state=absent'
# 傳輸文件到windows
ansible windows -m win_copy -a 'src=/etc/hosts dest=c:\test\hosts.txt'

4.3.4重啟

# 重啟 
ansible windows  -m win_reboot
ansible windows  -m win_shell -a 'shutdown -r -t 0'

4.3.5創建用戶

# 創建用戶
ansible 192.168.11.149 -m win_user -a "name=user1 passwd=123456"

4.4編寫playbook

ansible-windows模塊介紹:

Windows modules — Ansible Documentation

github示例網址:

https://github.com/ansible-collections/ansible.windows

創建一個目錄名稱為 playbook

# playbook語法檢查
ansible-playbook --syntax-check hello.yml 
# 會嘗試運行,然后進行語法檢查
ansible-playbook -C hello.yml# 執行playbook
ansible-playbook  hello.yml

4.4.1檢查是否連接成功

創建一個main.yaml文件

# 測試windows組是否連同
ansible windows -m win_ping# 這位寫是不能夠執行的
ansible windows -m ansible.windows.win_ping
- hosts: windowstasks:- name: Ensure connectivitywin_ping:

4.4.2創建一個文件

創建一個createDirectoryStructure.yaml

- name: test win_file modulehosts: windowstasks:- name: Create directory structureansible.windows.win_file:path: C:\Temp\folder\subfolderstate: directory

4.2.3更新文件

updateFileVersion.yaml

- name: Update burner version hosts: windowsvars:- burner_version: HiTrend_Flash_Programmer_int_v2.72.exetasks:- name: Delete old burnerwin_file:path: D:\Jenkins\HFP.exestate: absent- name: Copy new burnerwin_copy:src: /home/user/work/ansible/burner/{{burner_version}}dest: D:\Jenkins\HFP.exe

檢查文件語法是否合理:

ansible-playbook -C updateFileVersion.yaml# 執行playbook
ansible-playbook updateFileVersion.yaml

4.2.4部署代碼

主要功能:實現自動化部署python代碼的playbook

- name: Deployment codehosts: windowsvars:- burner_version: HiTrend_Flash_Programmer_int_v2.72.exetasks:- name: Delete old burner:path: D:\Jenkins\HFP.exestate: absent- name: Copy new burnerwin_copy:src: /home/user/work/ansible/burner/{{burner_version}}dest: D:\Jenkins\HFP.exe

5.踩坑記錄

5.1離線安裝python winrm模塊

有網絡的情況下安裝命令: pip install pywinrm

下載第三方庫,網址為: https://pypi.org/

離線安裝的第三方庫有:

pywinrm-0.4.2

ntlm-auth-1.5.0.tar.gz \ ntlm_auth-1.0.1-py2.py3-none-any.whl (建議使用)

requests_ntlm-1.1.0.tar.gz

xmltodict-0.12.0-py2.py3-none-any.whl

安裝第三方庫時候出現的坑:

1.Error in ntlm-auth setup command: Invalid environment marker: python_versions"2.7"

解決方案:

使用下載的 ntlm_auth-1.0.1-py2.py3-none-any.whl 方式進行安裝,這是因為我使用的是ntlm_auth-1.0.1-py2.py3-none-any.whl安裝方式進行安裝的。使用另外一種方式安裝后,成功。

2.but you’ ll have requests 2.6.0 which is incompatible

在安裝的過程中出現這些提示信息,需要使用相應的安裝包版本進行安裝,版本太低或者太高都不行。

3.Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError (' spip._vendor. ur11ib3. connection. HTTPSConnection object at 0x7f024a55c590x: Failed to establish a new connection: [ Errno 101] Network is unreachable',)': /simple/six/E

這個原因是因為在內網進行的安裝,不能夠連接到外部網絡,所以會出現這個錯誤,所以可以進行使用 ntlm-auth-1.5.0.tar.gz 的方式安裝第三方庫,然后進行查看具體的信息,需要依賴于其他的什么的第三方庫。

4.提示信息:不能找到相應的文件目錄

嘗試使用以下命令進行設置:

添加相應的 --target 信息

5.2以太網設置

Error: 由于此計算機上的網絡連接類型之一設置為公用,因此 WinRM 防火墻例外將不運行。 將網絡連接類型更改為域或專用,然后再次嘗試。

打開以太網設置后

將網絡設置為專用后就可以了,重新執行相關命令。

5.3連接不上被管理機器

1.“msg”: “plaintext: the specified credentials were rejected by the server”,

# 編輯主機的配置文件,添加以下字段
ansible_winrm_transport=ntlm

2.'msg‘: "ssl: the specified credentials were rejected by the server"

這個是因為在主機清單中缺少 ansible_ssh_port=5985 ,請仔細檢查主機清單中的配置信息

3.連接超時

這個可能用戶名或者密碼寫錯了。

5.4在cmd中可以運行windos命令,而playbook報錯

在cmd中運行的命令,并不能保證在powershell中可以運行。

總結

以上是生活随笔為你收集整理的Ansible在windows的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。