win_shell模块接受命令名,后跟一列空格分隔的参数。 它与win_command模块类似,但是通过目标主机上的shell(默认为PowerShell)运行该命令。
如果要安全且可预测地运行可执行文件,则最好使用win_command模块。编写剧本时的最佳做法将遵循使用win_command的趋势,除非win_shell明确要求。运行ad-hoc命令时,请使用您的最佳判断。
在创建的所有子进程都退出之前,WinRM不会从命令执行返回。因此,不可能用于win_shell生成长时间运行的子进程或后台进程。考虑创建用于管理后台进程的Windows服务。
参数 | 作用 |
---|---|
chdir | 运行command命令前先cd到这个目录 |
creates | 路径或路径过滤器模式; 当目标主机上存在引用的路径时,将跳过该任务。 |
removes | 路径或路径过滤器模式; 当目标主机上不存在引用的路径时,将跳过该任务。 |
free_form | 需要执行的脚本(没有真正的参数为free_form) |
stdin (2.5后新增) | 将命令的stdin设置为指定的值 |
executable | 指定执行shell是cmd还是powershell |
模块使用
#ansible 直接运行,获取主机名
[root@squid ~]# ansible windows -m win_shell -a 'whoami'
10.241.0.4 | SUCCESS | rc=0 >>
baiyongjie\administrator
#调用脚本
[root@squid ~]# ansible windows -m win_shell -a 'dir.bat chdir=C:\\at executable=cmd'
10.241.0.4 | SUCCESS | rc=0 >>
C:\at>dir 1>at.txt
#playbook中使用
[root@squid win_shell]# cat win_shell_args.yaml
---
- hosts: windows
tasks:
- name: run bat scrits
win_shell: dir.bat
args:
chdir: c:\at
executable: cmd
[root@squid win_shell]# ansible-playbook win_shell_args.yaml
PLAY [windows] ***************************************************************
TASK [run bat scrits] ********************************************************
changed: [10.241.0.4]
PLAY RECAP *******************************************************************
10.241.0.4 : ok=1 changed=1 unreachable=0 failed=0