使用complex-argsforms将自由格式的命令传递给Ansible

我正在使用编程方式生成Ansible的手册。 一般来说,因为剧本只是YAML,所以这很简单。 但是,当使用“简单” key=valueforms时,剧本不是纯粹的YAML – 它们包含embeddedshlex -parsableforms的内容。

为了避免这种forms的歧义(是key=value对命令的一个参数或ansible的参数),并且只有一种格式来parsing和生成,我无条件地使用示例演示的复杂的参数机制在例句库中 。

这使用以下types的语法:

 action: module-name args: key1: value1 key2: value2 

…这是好的,好的。 但是,当试图使用这种forms的shellcommand模块( 其文档将实际的命令描述为在名为free_form的参数中free_form )时,这并不是很好:

 action: shell args: free_form: echo hello_world >/tmp/something creates: /tmp/something 

当被调用时,这将运行以下内容:

 /bin/sh -c " free_form='echo hello_world >/tmp/something' " 

…这是非常不是我想要完成的。

使用纯YAML语法的“自由格式”命令使用Ansible模块的正确方法是什么?

简短的回答:不要使用commandrawscriptshell模块。 编写你自己的接受命令的模块作为“正常”参数。

很长的回答:

在大多数情况下,你可以这样做:

 - shell: echo hello_world > /tmp/something args: creates: /tmp/something 

但是,这在一些边缘情况下失败:

 - shell: echo hello_world > creates=something args: creates: creates=something # The file is named "creates=something" 

我不知道处理这个问题的一般方法,但是一个特定于bash的解决scheme是:

 - shell: echo hello_world > "creates=something" args: creates: creates=something 

现在在Ansible文档中解决了这个问题 。

 # You can also use the 'args' form to provide the options. This command # will change the working directory to somedir/ and will only run when # /path/to/database doesn't exist. - command: /usr/bin/make_database.sh arg1 arg2 args: chdir: somedir/ creates: /path/to/database 

请注意,没有名为“free_form”的参数。