我尝试添加自定义HBA条目以允许主机连接到Vagrant框上运行的PostgreSQL实例:
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
ansible.galaxy_role_file = "provisioning/requirements.yml"
ansible.host_vars = {
"default" => {
"postgresql_listen_addresses" => "*",
"postgresql_pg_hba_custom" => [{
"type" => "host",
"database" => "vagrant",
"user" => "vagrant",
"address" => "samenet",
"method" => "password"
}],
"database_name" => "vagrant",
"database_username" => "vagrant",
"database_password" => "password",
"node_environment" => "development"
}
}
end
我收到错误:
AnsibleUndefinedVariable:' unicode object'没有属性'键入'
如何在Vagrantfile 中传递主机变量的嵌套(复杂)配置值?
答案 0 :(得分:3)
检查主机变量如何在自动生成的库存文件中的主机条目的单行中编码,/ tmp / vagrant-ansible / inventory / vagrant_ansible_local_inventory:
# Generated by Vagrant default ... postgresql_pg_hba_custom=[{"type"=>"host", "database"=>"vagrant", "user"=>"vagrant", "address"=>"samenet", "method"=>"password"}] database_name=vagrant database_username=vagrant database_password=password node_environment=development
传递给postgresql_pg_hba_custom的值格式不正确。
Ansible documentation on variables州:
从Ansible 1.2开始,您还可以传入额外的变量作为引用的JSON,如下所示:
--extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'
因此,您应该将复数值作为JSON包装在单引号内传递。您可以使用Ruby的JSON module和字符串插值,而不是手动制作值:
config.vm.provision "ansible_local" do |ansible|
require 'json'
postgresql_pg_hba_custom = [{
"type" => "host",
"database" => "vagrant",
"user" => "vagrant",
"address" => "samenet",
"method" => "password"
}]
ansible.playbook = "provisioning/playbook.yml"
ansible.galaxy_role_file = "provisioning/requirements.yml"
ansible.host_vars = {
"default" => {
"postgresql_listen_addresses" => "*",
"postgresql_pg_hba_custom" => "'#{postgresql_pg_hba_custom.to_json}'",
"database_name" => "vagrant",
"database_username" => "vagrant",
"database_password" => "password",
"node_environment" => "development"
}
}
end
生成的库存文件现在为:
# Generated by Vagrant default ... postgresql_pg_hba_custom='[{"type":"host","database":"vagrant","user":"vagrant","address":"samenet","method":"password"}]' database_name=vagrant database_username=vagrant database_password=password node_environment=development