AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 584037
Accepted
Charles Duffy
Charles Duffy
Asked: 2014-03-24 14:43:59 +0800 CST2014-03-24 14:43:59 +0800 CST 2014-03-24 14:43:59 +0800 CST

使用 complex-args 形式将自由形式的命令传递给 Ansible

  • 772

我正在使用以编程方式生成的 Ansible 剧本。一般来说,因为 playbook 只是 YAML,所以这很简单。但是,当使用“简单”key=value表单时,剧本不是纯shlexYAML——它们包含嵌入可解析表单的内容。

为了避免这种形式的歧义(是key=value对命令的参数还是 ansible 的参数?),并且只有一种格式可以解析和生成,我无条件地使用ansible中示例演示的复杂 args 机制-示例存储库。

这使用以下类型的语法:

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

...这很好。但是,当尝试将这种形式用于shellorcommand模块时(其文档将实际命令描述为在名为 的参数中传递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 模块的正确方法是什么?

ansible
  • 2 2 个回答
  • 12282 Views

2 个回答

  • Voted
  1. Best Answer
    Snowball
    2014-06-14T08:44:08+08:002014-06-14T08:44:08+08:00

    简短回答:不要使用command、raw、script或shell模块。编写您自己的模块,将命令作为“正常”参数接受。

    长答案:

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

    - 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 的解决方案是:

    - shell: echo hello_world > "creates=something"
      args:
        creates: creates=something
    
    • 5
  2. Christian Long
    2016-04-20T12:02:35+08:002016-04-20T12:02:35+08:00

    现在在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”的参数。

    • 0

相关问题

  • Ansible 剧本,使用选项 --disabled-login 创建用户

  • ansible playbook,操作系统不同的用户

  • ansible:为什么文件模块会跳过?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve