9 Ví dụ về Playbooks Ansible dành cho Quản trị Windows

Tôi sẽ chỉ cho bạn một số thao tác mà quản trị viên có thể thực hiện trên hệ thống cửa sổ từ xa bằng cách sử dụng ansible-playbook.
Ansible là một trong những công cụ DevOps được sử dụng nhiều nhất trên thị trường hiện nay. Nó cung cấp một loạt các mô-đun cửa sổ được sử dụng để cấu hình và quản lý máy chủ Windows. Tôi giả sử bạn đã cài đặt Ansible trên Windows từ nơi bạn muốn quản lý các máy chủ Windows.
Sau đây là một số tác vụ thường được quản trị viên Windows thực hiện hàng ngày. Bạn sẽ ngạc nhiên khi thấy Windows được quản lý dễ dàng như thế nào bằng Ansible.
Địa chỉ IP của máy điều khiển Ansible Windows của tôi là 192.168.0.106 và địa chỉ IP của hệ thống Windows từ xa của tôi là 192.168.0.102. Trước khi bắt đầu, hãy đảm bảo bạn chạy mô-đun win_ping để kiểm tra xem bạn có thể kết nối với máy chủ từ xa của windows hay không.
[email protected] ~ $ ansible win -m win_ping 192.168.0.102 | SUCCESS => { "changed": false, "ping": "pong" }
Kết nối của tôi với máy chủ từ xa thành công.
Vì vậy, hãy bắt đầu với Ansible Playbooks…
Mục lục
Sao chép các tập tin
win_copy là một mô-đun ansible sao chép tệp từ máy chủ cục bộ sang máy chủ Windows từ xa. Tôi sẽ sử dụng mô-đun này để sao chép một tệp PDF.
Sử dụng mã YAML bên dưới, cung cấp đường dẫn nguồn và đích.
[email protected] ~ $ vi copy.yml --- - hosts: win tasks: - name: Copy File win_copy: src: C:output.pdf dest: C:ansible_examples remote_src: yes
Chạy ansible-playbook cho win_copy.
[email protected] ~ $ ansible-playbook copy.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Copy File] ***************************************************************************************************************************** changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Tệp đã được sao chép thành công tại vị trí đích trên hệ thống cửa sổ từ xa.
Cài đặt / gỡ cài đặt MSI
Để cài đặt ứng dụng bằng tệp MSI, bạn cần sử dụng win_get_url để đề cập đến đường dẫn của tệp MSI để tải xuống và sau đó sử dụng mô-đun win_package để cài đặt nó. Trạng thái hiện tại có nghĩa là MSI sẽ được cài đặt trên máy và ứng dụng ở trạng thái hiện tại.
Ở đây, tôi đang cài đặt Apache.
Mã YAML sẽ được sử dụng:
[email protected] ~ $ vi msi.yml --- - name: Installing Apache MSI hosts: win tasks: - name: Download the Apache installer win_get_url: url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi dest: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi - name: Install MSI win_package: path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi state: present
Chạy ansible-playbook để cài đặt bằng MSI.
[email protected] ~ $ ansible-playbook msi.yml PLAY [Installing Apache MSI] ***************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Download the Apache installer] ********************************************************************************************************* changed: [192.168.0.102] TASK [Install MSI] *************************************************************************************************************************** changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Bây giờ, hãy vào hệ thống windows và kiểm tra xem ứng dụng apache đã được cài đặt thành công chưa.
C:Userstechpoe.com>cd C:Program Files (x86)Apache Software FoundationApache2.2bin C:Program Files (x86)Apache Software FoundationApache2.2bin>httpd -v Server version: Apache/2.2.25 (Win32) Server built: Jul 10 2013 01:52:12
Bạn cũng có thể cài đặt các ứng dụng bằng MSI với các đối số. Dưới đây là ví dụ tương tự như trên, nhưng thay vì một trạng thái, chúng tôi đang sử dụng đối số cài đặt để cài đặt apache.
Mã YAML sẽ được sử dụng:
--- - name: Installing Apache MSI hosts: win tasks: - name: Download the Apache installer win_get_url: url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi dest: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi - name: Install MSI win_package: path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi arguments: - /install - /passive - /norestart
Để gỡ cài đặt ứng dụng bằng tệp MSI, bạn cần sử dụng mô-đun win_package. Trạng thái vắng mặt có nghĩa là ứng dụng sẽ được gỡ cài đặt bằng tệp MSI.
Ở đây, tôi đang gỡ cài đặt Apache.
[email protected] ~ $ vi uninstall_msi.yml --- - name: UnInstalling Apache MSI hosts: win tasks: - name: UnInstall MSI win_package: path: C:ansible_exampleshttpd-2.2.25-win32-x86-no_ssl.msi state: absent
Chạy ansible-playbook để gỡ cài đặt bằng MSI.
[email protected] ~ $ ansible-playbook uninstall_msi.yml PLAY [UnInstalling Apache MSI] ***************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [UnInstall MSI] ************************************************************************************************************************* changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Bây giờ, nếu tôi kiểm tra phiên bản apache, tôi sẽ nhận được kết quả bên dưới khi ứng dụng đã được gỡ cài đặt.
C:Program Files (x86)Apache Software FoundationApache2.2bin>httpd -v 'httpd' is not recognized as an internal or external command, operable program or batch file.
Gỡ cài đặt phần mềm (.EXE)
Bạn cũng có thể gỡ cài đặt phần mềm có tệp .exe bằng id sản phẩm của phần mềm đó.
[email protected] ~ $ vi uninstall.yml --- - hosts: win tasks: - name: Uninstall 7-Zip from the exe win_package: path: C:Program Files7-ZipUninstall.exe product_id: 7-Zip arguments: /S state: absent
Chạy ansible-playbook để gỡ cài đặt 7-Zip.
[email protected] ~ $ ansible-playbook uninstall.yml PLAY [win] ************************************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************************* ok: [192.168.0.102] TASK [Uninstall 7-Zip from the exe] *********************************************************************************************************************************************************** changed: [192.168.0.102] PLAY RECAP ************************************************************************************************************************************************************************************* 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Dừng / Bắt đầu / Khởi động lại Dịch vụ Windows
win_service ansible module được sử dụng để bắt đầu, dừng hoặc khởi động lại một dịch vụ. Ở đây, tôi sẽ chỉ cho bạn cách dừng dịch vụ tomcat.
Bạn cần đề cập đến tên dịch vụ trong tệp YAML và đặt trạng thái dừng.
[email protected] ~ $ vi service.yml --- - hosts: win tasks: - name: Stop service Tomcat win_service: name: Tomcat8 state: stopped
Chạy ansible-playbook để dừng dịch vụ tomcat.
[email protected] ~ $ ansible-playbook service.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Stop service Tomcat] **************************************************************************************************************** changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Nếu bạn kiểm tra dịch vụ tomcat trên hệ thống windows, nó hiện đang ở trạng thái dừng.
Bạn có thể xác định trạng thái để bắt đầu hoặc khởi động lại hoặc tạm dừng để thay đổi trạng thái của dịch vụ.
Thu thập sự kiện
Sử dụng mô-đun ansible win_disk_facts, bạn có thể truy xuất tất cả thông tin đĩa của máy chủ đích.
[email protected] ~ $ vi disk.yml --- - hosts: win tasks: - name: Get disk facts win_disk_facts: - name: Output first disk size debug: var: ansible_facts.disks[0].size - name: Convert first system disk into various formats debug: msg: '{{ disksize_gib }} vs {{ disksize_gib_human }}' vars: # Get first system disk disk: '{{ ansible_facts.disks|selectattr("system_disk")|first }}' # Show disk size in Gibibytes disksize_gib_human: '{{ disk.size|filesizeformat(true) }}' disksize_gib: '{{ (disk.size/1024|pow(3))|round|int }} GiB'
Chạy ansible-playbook để lấy thông tin đĩa.
[email protected] ~ $ ansible-playbook disk.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Get disk facts] ************************************************************************************************************************ ok: [192.168.0.102] TASK [Output first disk size] **************************************************************************************************************** ok: [192.168.0.102] => { "ansible_facts.disks[0].size": "1000204886016" } TASK [Convert first system disk into various formats] **************************************************************************************** ok: [192.168.0.102] => { "msg": "932 GiB vs 931.5 GiB" } PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sử dụng mô-đun ansible win_command, bạn có thể thực hiện các lệnh trên máy chủ từ xa và nhận thông tin CPU, chi tiết thiết bị, v.v.
wdzwd[email protected] ~ $ vi check.yml --- - hosts: win tasks: - name: Get disk facts win_command: wmic cpu get caption, deviceid, name, numberofcores, maxclockspeed, status register: usage - debug: msg="{{ usage.stdout }}"
Chạy ansible-playbook để nhận thông tin hệ thống từ xa.
[email protected] ~ $ ansible-playbook check.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Get facts] ************************************************************************************************************************ changed: [192.168.0.102] TASK [debug] ********************************************************************************************************************************* ok: [192.168.0.102] => { "msg": "Caption DeviceID MaxClockSpeed Name NumberOfCores Status rrnIntel64 Family 6 Model 142 Stepping 9 CPU0 2712 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2 OK rrnrrn" } PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Chạy lệnh
Bất kỳ lệnh nào bạn chạy trên một cửa sổ, chúng có thể được chạy qua mô-đun win_command ansible. Bạn chỉ cần chỉ định lệnh trong tệp YAML của mình. Ở đây, tôi chỉ đang tạo một thư mục.
[email protected] ~ $ vi commands.yml --- - hosts: win tasks: - name: run an executable using win_command win_command: whoami.exe - name: run a cmd command win_command: cmd.exe /c mkdir C:test
Chạy ansible-playbook để thực hiện thao tác win_command.
[email protected] ~ $ ansible-playbook commands.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [run an executable using win_command] *************************************************************************************************** changed: [192.168.0.102] TASK [run a cmd command] ********************************************************************************************************************* changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Các biến môi trường
Hệ thống cửa sổ có nhiều biến môi trường, ví dụ, JAVA_HOME. Sử dụng mô-đun ansible win_enosystem, bạn có thể thêm hoặc sửa đổi các biến môi trường trên hệ thống windows. Trong ví dụ này, tôi đang thêm một biến mới vào danh sách các biến môi trường windows.
[email protected] ~ $ vi env.yml --- - hosts: win tasks: - name: Set an environment variable for all users win_environment: state: present name: NewVariable value: New Value level: machine
Chạy ansible-playbook để thêm biến môi trường trên máy windows từ xa.
[email protected] ~ $ ansible-playbook env.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Set an environment variable for all users] ********************************************************************************************* changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Chuyển đến cửa sổ biến môi trường; bạn sẽ thấy biến mới bạn vừa thêm hiện diện ở đây.
Thêm / chỉnh sửa sổ đăng ký
win_regedit mô-đun ansible được sử dụng để thêm hoặc chỉnh sửa các chi tiết đăng ký trên máy windows từ xa. Bạn cần cung cấp đường dẫn của sổ đăng ký và nội dung được thêm / cập nhật. Ở đây tôi đang tạo một mục đăng ký mới techpoe.com bên trong đường dẫn HKLM: SOFTWARE và sau đó thêm tên và dữ liệu vào sổ đăng ký này.
[email protected] ~ $ vi registry.yml --- - hosts: win tasks: - name: Creating a registry win_regedit: path: HKLM:SOFTWAREtechpoe.com - name: Modifying a registry, adding name and data win_regedit: path: HKLM:SOFTWAREtechpoe.com name: Geek data: Flare
Chạy ansible-playbook để thêm sổ đăng ký.
[email protected] ~ $ ansible-playbook registry.yml PLAY [win] *********************************************************************************************************************************** TASK [Gathering Facts] *********************************************************************************************************************** ok: [192.168.0.102] TASK [Creating a registry] ******************************************************************************************************************* changed: [192.168.0.102] TASK [Modifying a registry, adding name and data] ******************************************************************************************** changed: [192.168.0.102] PLAY RECAP *********************************************************************************************************************************** 192.168.0.102 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Nếu bạn vào Registry Editor trên hệ thống từ xa, bạn có thể thấy sổ đăng ký này đã được thêm thành công với các tham số Tên và Dữ liệu.
Xóa nhật ký
win_eventlog ansible module được sử dụng để thêm, xóa hoặc xóa các bản ghi sự kiện windows khỏi hệ thống windows.
Đi tới Windows Powershell và liệt kê các EventLog có trên máy tính cửa sổ từ xa.
PS C:Userstechpoe.com> Get-EventLog -List Max(K) Retain OverflowAction Entries Log ------ ------ -------------- ------- --- 20,480 0 OverwriteAsNeeded 33,549 Application 20,480 0 OverwriteAsNeeded 0 HardwareEvents 512 7 OverwriteOlder 20 Internet Explorer 20,480 0 OverwriteAsNeeded 0 Key Management Service 128 0 OverwriteAsNeeded 190 OAlerts Security 20,480 0 OverwriteAsNeeded 44,828 System 15,360 0 OverwriteAsNeeded 3,662 Windows PowerShell
Bây giờ, tôi sẽ hướng dẫn cách xóa nhật ký khỏi tất cả các nguồn cho Internet Explorer.
[email protected] ~ $ vi log.yml --- - hosts: win tasks: - name: Remove Internet Explorer Logs win_eventlog: name: Internet Explorer state: absent
Chạy ansible-playbook để loại bỏ hình thức Internet Explorer của máy cửa sổ từ xa.
[email protected] ~ $ ansible-playbook log.yml PLAY [win] ************************************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************************* ok: [192.168.0.102] TASK [Remove Internet Explorer Logs] ********************************************************************************************************************************************** changed: [192.168.0.102] PLAY RECAP ************************************************************************************************************************************************************************************* 192.168.0.102 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Bây giờ, nếu bạn liệt kê lại EventLogs, bạn sẽ thấy các nhật ký Internet Explorer đã bị xóa.
PS C:Userstechpoe.com> Get-EventLog -List Max(K) Retain OverflowAction Entries Log ------ ------ -------------- ------- --- 20,480 0 OverwriteAsNeeded 33,549 Application 20,480 0 OverwriteAsNeeded 0 HardwareEvents 20,480 0 OverwriteAsNeeded 0 Key Management Service 128 0 OverwriteAsNeeded 190 OAlerts Security 20,480 0 OverwriteAsNeeded 44,835 System 15,360 0 OverwriteAsNeeded 56 Windows PowerShell
Vì vậy, đó là tất cả về sách chơi Ansible, có thể được sử dụng để quản trị cửa sổ từ xa. Hãy tiếp tục và thử những cuốn sách chơi này. Bạn cũng có thể thử khác Mô-đun Windows Ansible có sẵn.