Ansible:如何向“命令"添加变量或“壳" [英] Ansible: How to add variables to "command" or "shell"

查看:18
本文介绍了Ansible:如何向“命令"添加变量或“壳"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 commandshell 模块上使用变量?我有以下代码,我想使用变量文件来提供一些配置:

Is it possible to use variables on command or shell modules? I have the following code, and I would like to use variable file to provide some configurations:

我想从我的变量文件中读取 Hadoop 版本.在 ansible 的其他模块上,我可以使用 {{ansible_version}},但使用命令或 shell 则不起作用.

I would like to read the Hadoop version from my variables file. On other modules of ansible I could use {{ansible_version}}, but with command or shell it doesn't works.

- name: start ZooKeeper HA
  command: hadoop-2.7.1/bin/hdfs zkfc -formatZK -nonInteractive

- name: start zkfc
  shell: hadoop-2.7.1/sbin/hadoop-daemon.sh start zkfc

我想转换为以下内容:

- name: Iniciar zkfc
  command: {{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc

因为如果我用这个语法运行它会抛出这个错误:

Because if I run the with this syntax it throws this error:

- name: inicializar estado ZooKeeper HA
  command: {{hadoop_version}}/bin/hdfs zkfc -formatZK -nonInteractive
                             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我尝试使用,但同样的问题:

I have try using, but same problem:

- name: Iniciar zkfc
  command: "{{ hadoop_version }}"/sbin/hadoop-daemon.sh start zkfc

正确的语法是什么?

推荐答案

command 参数中引用完整的字符串:

Quote the full string in the command argument:

- name: Iniciar zkfc
  command: "{{ hadoop_version }}/sbin/hadoop-daemon.sh start zkfc"

这篇关于Ansible:如何向“命令"添加变量或“壳"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆