<p>使用以下文件在我的机器上进行测试后:</p>
<pre class="lang-rb prettyprint-override"><code> Vagrant.configure(2) do |config|
config.vm.box = "generic/ubuntu1904"
config.vm.hostname = "test"
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end
config.vm.provision "default", type: "shell", inline: "which python", run: "always"
end
</code></pre>
<p>这是<code>vagrant up</code>(仅最后一行)的结果</p>
<pre><code>==> default: Running provisioner: default (shell)...
default: Running: inline script
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
</code></pre>
<p>交互式检查:</p>
<pre><code>$ vagrant ssh
Last login: Wed Oct 9 15:33:22 2019 from 10.0.2.2
vagrant@test:~$ which python
vagrant@test:~$ echo $?
1
vagrant@test:~$ which python3
/usr/bin/python3
vagrant@test:~$ echo $?
0
vagrant@test:~$
</code></pre>
<p>结论:你得到的是完全一致的。<code>python</code>在您的ubuntu映像中不存在,因此您的命令的其余部分将运行。你的场景有一个缺陷,你需要找到另一种方法。你知道吗</p>
<p>在你的背景下,我会尝试在ansible中运行所有东西。这里有一个例子,只是为了这个想法,我没有测试,这肯定可以大大改善</p>
<pre><code>- name: Make sure machine can run ansible
hosts: all
gather_facts: false
tasks:
- block:
- name: Try to ansible-ping the host. Consider python is not installed otherwise
ping:
rescue:
- name: No python available, install with low-level and dirty command
become: true
become_method: sudo
raw: pacman noconfirm -S python
</code></pre>