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

问题描述 投票:0回答:1

我遇到了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

我想将它作为一次性任务使用,所以我想以风格使用ad-hoc执行

ansible <host> -m <module> -a <args...>

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

将命名参数放在free_form参数之后将所有内容放在free_form参数中,导致powershell抱怨无关的参数

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

PS:我知道我可能在free_form参数中填充脚本路径和参数,但是我更感兴趣的是学习ad-hoc是否可行,因为文档没有说出任何一种方式。

ansible ansible-ad-hoc
1个回答
1
投票

我不能直接测试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
© www.soinside.com 2019 - 2024. All rights reserved.