如何为 ansible ad-hoc 提供命名和 free_form 参数?

How to supply both named and free_form arguments to ansible ad-hoc?

我 运行 跨越 Ansible 的一个模块,该模块采用 free_form 个参数以及命名参数 - win_command。举个具体的例子,在stdin:

上提供powershell脚本
- name: Run an executable and send data to the stdin for the executable
  win_command: powershell.exe -
  args:
    stdin: Write-Host test

我想将其用作一次性任务,所以我想使用

风格的临时执行
ansible <host> -m <module> -a <args...>

不幸的是,我在 documentation 中没有看到关于如何处理需要同时指定 free_form 和命名参数的模块的信息。有人知道吗?

将命名参数放在 free_form 参数之后会将所有内容都放在 free_form 参数中,导致 powershell 抱怨 ext运行eous arguments

... -m win_command -a 'powershell - stdin=C:\some\script.ps1 -arg1 value_1 -arg2 value_2'

PS:我知道我可能会在 free_form 参数中同时填充脚本路径和参数,但我更感兴趣的是了解这是否可以临时实现,因为文档没有说明任何一种方式。

我不能直接测试win_command模块,但是用语法上非常相似的command模块,你可以重现这个:

- command: some_command
  args:
    chdir: /tmp
    creates: flagfile

像这样:

ansible -m command -a 'chdir=/tmp creates=flagfile some_command'

更新

经调查...您在 stdin 中遇到的问题不是引用问题; 就是当使用 k1=v1 k2=v2 somecommand 格式将参数传递给例如command 模块,Ansible 只处理特定的键。在 lib/ansible/parsing/splitter.py 中我们看到:

if check_raw and k not in ('creates', 'removes', 'chdir', 'executable', 'warn'):
    raw_params.append(orig_x)
else:
    options[k.strip()] = unquote(v.strip())

也就是说,它只识别 createsremoveschdirexecutablewarn 作为模块参数。我认为这是 Ansible 中的一个错误。添加对 stdin 参数的支持是微不足道的,当然:

if check_raw and k not in ('stdin', 'creates', 'removes', 'chdir', 'executable', 'warn'):

通过此更改,我们可以按预期包含带空格的 stdin

$ ansible localhost -m command -a 'chdir=/tmp stdin="Hello world" sed s/Hello/Goodbye/'                                                                    
 [WARNING]: Unable to parse /home/lars/.ansible_hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

localhost | CHANGED | rc=0 >>
Goodbye world