简体   繁体   English

ansible 脚本“SHELL”与在 ubuntu 机器 20.04 EC2 上直接运行 Shell 命令的细微差别?

[英]The subtle difference in an ansible script "SHELL" and the directly running Shell commands on an ubuntu machine 20.04 EC2?

I come with a Vagrant script that pulls down an bionic 18 which then runs an ansible playbook which generates two EC2s ubuntu 20.04 which successfully run though every "task:" I assign.我附带了一个 Vagrant 脚本,它可以拉下一个仿生 18,然后运行一个 ansible 剧本,该剧本生成两个 EC2 ubuntu 20.04,它成功运行了我分配的每个“任务:”。 I am able to run everything I want in a largely automated download and execution for a publisher subscriber method.我能够在发布者订阅者方法的很大程度上自动下载和执行中运行我想要的一切。 Here is the issue: I can run my.sh and.py scripts manually and the systems works, but when I use the ansible methods I must be doing something wrong much like these solutions point to:这是问题所在:我可以手动运行 my.sh 和 .py 脚本并且系统可以正常工作,但是当我使用 ansible 方法时,我一定是做错了,就像这些解决方案指向的那样:

What I want to do is simply correct the issue with this, and run it in the background.我想做的只是纠正这个问题,然后在后台运行它。

- name: Start Zookeeper
  shell: sudo /usr/local/kafka-server/bin/zookeeper-server-start.sh  /usr/local/kafka-server/config/zookeeper.properties </dev/null >/dev/null 2>&1 &
- name: Sleep for 30 seconds and continue with play
  wait_for:
    timeout: 15
- name: Start Kafka broker
  shell: sudo /usr/local/kafka-server/bin/kafka-server-start.sh  /home/ubuntu/usr/local/kafka-server/config/server.properties </dev/null >/dev/null 2>&1 &

I have tried it with just a single "&" at the end as well as passing in explicit calls to my user account "ubuntu" .我试过最后只用一个“&” ,并将显式调用传递给我的用户帐户“ubuntu” I've used the "become: yes" .我用过"become: yes" I really don't want to use a daemon especially since others seem to have used this successfully before.我真的不想使用守护进程,尤其是因为其他人之前似乎已经成功地使用过它。

I do want to note that a glaring sign to you that I can't seem to think through is that it hangs when I don't include the &, but if I do include the & it just outright fails, which made me think it was running, but the script won't proceed because these are listener processes.我确实想指出,一个我似乎无法想通的明显迹象是,当我不包含 & 时它会挂起,但如果我确实包含 &,它就会彻底失败,这让我想到了正在运行,但脚本不会继续,因为这些是侦听器进程。

#     - name: Start Zookeeper
#       become: yes
#       script: /usr/local/kafka-server/bin/zookeeper-server-start.sh
#       args: 
#         chdir: /usr/local/kafka-server/config/zookeeper.properties

This failed, and I'd rather not create another script to copy over the directories and localize it if there is a simple solution to the first block of code.这失败了,如果第一块代码有一个简单的解决方案,我宁愿不创建另一个脚本来复制目录并对其进行本地化。

Multiple ways to skin this cat, but I'd rather just have my mistake on the shell ansible command fixed, and I don't see it.有多种方法可以给这只猫蒙皮,但我宁愿只修复我在 shell ansible 命令上的错误,但我没有看到它。

As explained in the answers written in your third link:正如您在第三个链接中所写的答案中所解释的那样:

https://superuser.com/questions/870871/run-a-remote-script-application-in-detached-mode-in-ansible https://superuser.com/questions/870871/run-a-remote-script-application-in-detached-mode-in-ansible

This happens because the script process is a child process from the shell spawned by ansible. To keep the process running after ansible has finished you would need to disown this child process.发生这种情况是因为脚本进程是 ansible 产生的 shell 的子进程。要在 ansible 完成后保持进程运行,您需要放弃此子进程。

The proper way to do this is configuring the software (zookeeper in your case) as a service.执行此操作的正确方法是将软件(在您的情况下为 zookeeper)配置为服务。 There are plenty of examples for this such as:有很多这样的例子,例如:

https://askubuntu.com/questions/979498/how-to-start-a-zookeeper-daemon-after-booting-under-specific-user-in-ubuntu-serv https://askubuntu.com/questions/979498/how-to-start-a-zookeeper-daemon-after-booting-under-specific-user-in-ubuntu-serv

Once you have configured it as a service, you can start it or stop it using ansible service module.将其配置为服务后,您可以使用 ansible 服务模块启动或停止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM