我正在尝试将字符串映射传递到打包器中的 ansible 配置程序中。
{
alex = "alexano"
bob = "bobvim"
}
我遇到了这个问题如何在Packer中将变量设置为列表并将其用作参考,但它退出时出现错误
ansible-playbook: error: unrecognized arguments: {ssh_users:bob:bob-vim}
在打包器中使用
--extra-vars
传递字符串映射的正确方法是什么?
provisioner "ansible-local" {
playbook_dir = "./ansible"
playbook_file = "./ansible/ami_9_x86_64.yaml"
galaxy_file = "./ansible/requirements.yaml"
only = ["amazon-ebs.bastion-server"]
extra_arguments = [
"-vv",
"--extra-vars",
jsonencode({ "ssh_users" = var.ssh_users}),
]
}
这是打包器构建命令输出片段(为简洁起见,已截断)。
Executing Ansible:
ansible-playbook /tmp/packer-provisioner-ansible-local/*************/ami_9_x86_64.yaml \
--extra-vars "packer_build_name=bastion-server packer_builder_type=amazon-ebs \
packer_http_addr=ERR_HTTP_ADDR_NOT_IMPLEMENTED_BY_BUILDER -o IdentitiesOnly=yes" \
-vv --extra-vars {"ssh_users":{"alex":"alex-nano","bob":"bob-vim"}} -c local \
-i /tmp/packer-provisioner-ansible-local/********/packer-provisioner-ansible-local2248890322
根据给定构造的命令行输出
-vv --extra-vars {"ssh_users":{"alex":"alex-nano","bob":"bob-vim"}}
缺少单引号。请参阅Ansible:如何在
ansible-playbook --extra-vars=
中的变量中传递JSON下的答案,以及以下最小示例剧本
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Show provided content
debug:
msg: "{{ ssh_users }}"
通过
调用ansible-playbook extra.yml --extra-vars={"ssh_users":{"alex":"alex-nano","bob":"bob-vim"}}
将产生
的输出TASK [Show provided content] ******
fatal: [localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'ssh_users' is undefined. 'ssh_users' is undefined
并通过
致电ansible-playbook extra.yml --extra-vars '{"ssh_users":{"alex":"alex-nano","bob":"bob-vim"}}'
结果为
的输出TASK [Show provided content] ******
ok: [localhost] =>
msg:
alex: alex-nano
bob: bob-vim
总而言之,
jsonencode({ "ssh_users" = var.ssh_users})
的输出需要用单引号引起来。