【问题标题】:ansible shell Linuxansible shell Linux
【发布时间】:2020-07-29 20:08:31
【问题描述】:

好的,下面我从一个文件中读取了一行,该文件是一个带参数的应用程序。我阅读它是为了了解如何从 ansible 在 Linux 服务器上运行。我使用 ansible 读取该行,但不知道如何使用 shell: 或 command: 运行它。或者这不是使用ansible的正确方法。如果我 -shell: echo "{{ connection.stdout }}" 它会回显该行,但无法弄清楚如何运行,就好像我在使用命令行的那台机器上一样。

name: extract command from file

slurp:

src: /home/ansible/con.txt

register: connection

shell: content="{{ connection.stdout }}"

我可以回显 connection.stdout 并返回我需要运行的行,但不知道如何在机器上运行它。文件在那里,我读取了文件并可以在文件中回显该行。只需要能够在机器上运行该行即可。
谢谢

【问题讨论】:

  • 由于某种原因,它不允许我在帖子中正确格式化
  • 也许使用 ``` ``` 其中 ``` 在换行符上。
  • @TyBoard 在创建/编辑答案时,您在编辑器区域顶部有格式化按钮 + 在屏幕右侧有帮助。这应该可以帮助您正确地格式化您的问题(这对于理解您当前的问题是非常必要的)。

标签: linux shell command-line ansible


【解决方案1】:

假设您运行以下代码:

- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
  shell: somescript.sh >> somelog.txt

然后它会执行 somescript.sh 并将脚本生成的输出写入 somelog.txt

通过您的描述,我假设您这样做:

- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
  shell: echo "somescript.sh >> somelog.txt"

然后执行 echo 打印 somescript.sh >> somelog.txt

【讨论】:

  • con.txt 文件包含一个应用程序,其中包含我需要在系统上执行的参数。我只是不知道如何从文件中获取命令并在机器上执行它。
  • 另一个应用程序完成,它吐出的最后一件事是我需要在其他服务器上运行的命令。我将它传送到 con.txt,以便我可以将其复制到其他服务器然后运行它。我只是不知道如何从服务器上的文件中运行它。
  • 谢谢,我需要休息一下,因为我发现自己做错了什么。
  • 啊,我明白了。如果只有命令在 con.txt 中,那么 ... shell: $(echo con.txt) 应该可以工作。然后 echo 将“提取”命令并 $() 将执行它。 unix.stackexchange.com/questions/147420/what-is-in-a-command 可能会描述得更好一点。
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 2013-12-09
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多