所以我有一个 ansible 剧本如下:
#WINDOWS#
---
- hosts: windows tasks:
- name: copy file
raw: '"net use M: "\\somemachinename\someLocation" /user:username password"'
raw: '"xcopy M:\isntaller.exe C:\installerlocation /Y"'
raw: '"net use M: /delete /y"'
该文件确实存在于网络位置并且用户名密码有效。该任务不会报告任何错误。但该文件永远不会被复制。
有谁知道我的剧本语法是否错误?或者是否有更好的方法在 ansible 设置中跨网络位置获取文件?
附注我无法访问 ansible 服务器。虽然我知道它是 Red Hat Linux 服务器。
第三个
raw
覆盖第一个和第二个,因为它们处于同一任务中。请参阅 YAML 语法概述。
将其分成 3 个独立的任务:
#WINDOWS#
---
- hosts: windows
tasks:
- name: mount M
raw: '"net use M: "\\somemachinename\someLocation" /user:username password"'
- name: copy file
raw: '"xcopy M:\isntaller.exe C:\installerlocation /Y"'
- name: unmount M
raw: '"net use M: /delete /y"'
另外,我不确定引号和双引号。你可能吃得太多了。
尝试了很多组合,这最终对我有用。看起来很老套,但可能对其他可能面临同样情况的人有帮助。
我在 http 位置添加了一个批处理文件,ansible 可以在该位置获取它并在机器上执行。
REM @echo off
set Source=%1%
set Destination=%2%
net use M: \\nwb-somemachinename /user:username password
xcopy /F M:\%Source% %Destination% /Y
net use M: /delete /y
yml 文件如下所示:
#WINDOWS#
---
- hosts: windows
tasks:
- name: get batch file
raw: httptool.exe http://somelocation/ourbatchfile.bat
- name: run batch file
raw: c:\location\ourbatchfile.bat location_remote_machine\installer.exe c:\location
以下对我有用:
---
- name: Map network drive Z with credentials and copy to C:\autodeploy
block:
- name: Save the network credentials required for the mapped drive
community.windows.win_credential:
name: network-cred
type: domain_password
username: '{{ ansible_user }}'
secret: '{{ ansible_password }}'
state: present
- name: Create a mapped drive Z
community.windows.win_mapped_drive:
letter: Z
path: '\\10.0.0.169\SmbShare'
username: '{{ ansible_user }}'
password: '{{ ansible_password }}'
state: present
- name: Ensure drive Z is persistent
ansible.windows.win_command:
cmd: 'net use Z: \\10.0.0.169\SmbShare /persistent:yes'
#Copy contents from Z:\installers to C:\autodeploy
- name: Ensure C:\autodeploy exists
ansible.windows.win_file:
path: C:\autodeploy
state: directory
- name: Copy all contents from UNC path to C:\autodeploy
ansible.windows.win_copy:
src: \\10.0.0.169\SmbShare\installers
dest: C:/autodeploy
remote_src: true
recurse: true
vars:
ansible_become: yes
ansible_become_method: runas
ansible_become_user: '{{ ansible_user }}'
ansible_become_pass: '{{ ansible_password }}'