Ansible variable from packer script

I have one variable in ansible script like

- host:{{host}}

I want to send {{host}} variable value from packer script. I want to send {{host}} value using packer build or using packer variable. Is there anyway do it?

Upvotes: 2

Views: 5681

Answers (2)

Lamine BA
Lamine BA

Reputation: 129

Below a simple example:

...
variable "gitlab_version" {
  type    = string
  default = "15.1.6"
}
...

build {
  name = local.build_name

  provisioner "ansible" {
    ...
    playbook_file = "./ansible/playbook.yml"
    extra_arguments = ["--extra-vars", "gitlab_version=${var.gitlab_version}"]
    ...
  }
}

It works as it's a simple interpolation

Upvotes: 0

Sam
Sam

Reputation: 2882

Using an ansible provisioner in packer allows you to use both ansible_env_vars and extra_arguments.

See doco: https://www.packer.io/plugins/provisioners/ansible/ansible#configuration-reference

So we generally used extra_arguments to pass in ansible variables over the command line

{
  "type": "ansible",
  "playbook_file": "./my_playbook}",
  "extra_arguments": "-vvv --extra-vars 'host={{user `host`}}" 
}

Upvotes: 4

Related Questions