gpt4 book ai didi

vagrant - 无法将嵌套的哈希数组传递给 Vagrant 的 ansible_local 配置程序中的 ansible.host_vars

转载 作者:行者123 更新时间:2023-12-02 21:04:24 24 4
gpt4 key购买 nike

我正在尝试添加自定义 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' has no attribute 'type'

如何在我的 Vagrantfile 内部传递主机变量的嵌套(复杂)配置值?

最佳答案

检查自动生成的 list 文件/tmp/vagrant-ansible/inventory/vagrant_ansible_local_inventory 中的主机条目的主机变量如何在单行中编码:

# Generated by Vagrantdefault ... 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

The value passed to postgresql_pg_hba_custom is not in a valid format.

The Ansible documentation on variables states:

As of Ansible 1.2, you can also pass in extra vars as quoted JSON, like so:

--extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'

So, you should pass complex values as JSON wrapped inside single-quotes. Instead of manually crafting the value, you can make use of Ruby's JSON module and string interpolation:

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

生成的 list 文件现在将是:

# Generated by Vagrantdefault ... 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

关于vagrant - 无法将嵌套的哈希数组传递给 Vagrant 的 ansible_local 配置程序中的 ansible.host_vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com