如何将字符串映射传递给打包器中的ansible

问题描述 投票:0回答:1

我正在尝试将字符串映射传递到打包器中的 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
ansible packer
1个回答
0
投票

根据给定构造的命令行输出

-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})
的输出需要用单引号引起来。

© www.soinside.com 2019 - 2024. All rights reserved.