我正在使用vagrant和ansible来创建和提供环境。我已经把一切都搞好了,但我发现有几个命令花了很长时间才能执行。由于ansible没有提供查看shell命令的实时输出的方法,因此我决定将这些命令分离出来以分离shell脚本并将它们作为shell配置程序执行,以便查看它们的输出。当我通过将shell配置程序放在Vagrantfile的 end (Ansible配置程序之后)来实验该过程时,这种方法很有效,但是如果我打破了这个过程就会导致问题。这是一个高级的伪示例:
我有3个剧本:setup.yml,post-download.yml和post-sample-data.yml
所需的流程如下:
Vagrantfile
provisioner: "ansible", playbook "setup.yml"
- Tasks...
- Create shell scripts for upcoming shell provisioners...
- meta: end_play
provisioner: "shell", inline: "bin/bash /path/to/created/shell/script"
(run script)
provisioner: "ansible", playbook "post-download.yml"
- Tasks...
- meta: end_play
provisioner: "shell", inline: "bin/bash /path/to/created/shell/script"
(run script)
provisioner: "ansible", playbook "post-sample-data.yml"
- Tasks...
- meta: end_play
provisioner: "shell", inline: "bin/bash /path/to/created/shell/script"
(run script)
end
当我考虑到这个想法运行vagrant provision
时,我在第一次shell配置程序尝试时遇到以下错误:
/tmp/vagrant-shell: line 1: bin/sh: No such file or directory
根据错误消息,我的假设是,在Vagrantfile执行期间,vagrant shell无法对服务器上所做的更改做出反应;因此,在初始ansible配置程序运行后,它无法找到创建的shell脚本作为配置程序运行。这是正在发生的事情,还是有办法使这种方法有效?
如果它有帮助,这是我的vagrantfile中的实际代码:
# Kick off the pre-install Ansible provisioner
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/setup.yml"
end
# Kick off the installation, and sample data shell scripts so we can get terminal output
if settings['project']['install_method'] == 'install' || settings['project']['install_method'] == 'reinstall'
config.vm.provision "shell", inline: "bin/sh #{settings['installation']['directory']}/download.sh"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/post-download.yml"
end
config.vm.provision "shell", inline: "bin/sh #{settings['installation']['directory']}/install.sh"
end
# Kick off the sample data shell script to download the sample data packages so we can get terminal output
if settings['use_sample_data'] == true
config.vm.provision "shell", inline: "bin/sh #{settings['installation']['directory']}/sample-data.sh"
end
# Kick off the post-sample-data Ansible provisioner
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/post-sample-data.yml"
end
# Kick off the cache warmer script so we can get terminal output
if settings['project']['warm_cache'] == true
config.vm.provision "shell", inline: "/bin/sh #{settings['installation']['directory']}/cache-warmer.sh"
end
答案 0 :(得分:1)
感谢上面@tux的评论,我可以确认,只要ansible项目结构合理,这种方法就可以在playbooks之间显示输出。
对于那些好奇的人,这里是Vagrantfile的更新版本:
# Kick off the pre-install Ansible provisioner
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/setup.yml"
end
# Kick off the installation, and sample data shell scripts so we can get terminal output
if settings['project']['install_method'] == 'install' || settings['project']['install_method'] == 'reinstall'
config.vm.provision "shell", privileged: false, inline: "/bin/sh #{settings['installation']['directory']}/download.sh"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/post-download.yml"
end
config.vm.provision "shell", privileged: false, inline: "/bin/sh #{settings['installation']['directory']}/install.sh"
end
# Kick off the sample data shell script to download the sample data packages so we can get terminal output
if settings['use_sample_data'] == true
config.vm.provision "shell", privileged: false, inline: "/bin/sh #{settings['installation']['directory']}/sample-data.sh"
end
# Kick off the post-sample-data Ansible provisioner
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/post-sample-data.yml"
end
# Kick off the cache warmer script so we can get terminal output
if settings['project']['warm_cache'] == true
config.vm.provision "shell", inline: "/bin/sh #{settings['installation']['directory']}/cache-warmer.sh"
end
请注意在除最后一个脚本配置程序之外的所有脚本配置文件中使用privileged: false
。如果您不希望希望以root用户身份执行脚本,则必须执行此操作。