假设我有一个剧本,我通常按如下方式运行:
ansible-playbook -i hosts -e "var1=val1" \
-e "hosts=all" \
-e "devops_dir=$DEVOPS_PATH" \
-e "cname=something.gooten.com" \
-v playbook.yml
我想在启动 Vagrantfile 时运行它--
#
# Run Ansible from the Vagrant Host
#
config.ssh.insert_key = false
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.verbose = "v"
end
如何将
-e
变量添加到 Vagrantfile
中以便它们也被调用?
extra_vars
ansible.extra_vars = {
var1: val1,
hosts: all,
devops_dir: $DEVOPS_PATH,
cname: something.gooten.com
}
如果你希望能够将任意参数传递给 ansible,比如说
-e
args 以及 -t
、-v
等,你可以使用这个技巧:
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
# use args from `ANSIBLE_ARGS` env variable
ansible.raw_arguments = Shellwords.shellsplit(ENV['ANSIBLE_ARGS']) if ENV['ANSIBLE_ARGS']
end
然后,像这样调用
vagrant provision
:
ANSIBLE_ARGS="-v -e var1=val1 -t mytag" vagrant provision