Issue passing variables to Ansible from Packer

1,243 views
Skip to first unread message

D Welch

unread,
May 20, 2016, 9:35:08 PM5/20/16
to Packer
I am trying to pass a single variable from Packer (0.10.0) to Ansible (2.0.2.0) and am having no luck. 

Here's my packer server.json file:

{
  "variables": {
    "aws_access_key": "XXX",
    "aws_secret_key": "XXX",
    "silo": "staging"
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "us-west-2",
    "source_ami": "XXX",
    "instance_type": "m1.small",
    "ssh_username": "ubuntu",
    "ami_name": "XXX {{timestamp}}"
  }],
  "provisioners": [
    {
      "type": "ansible",
      "playbook_file": "./server.yml",
      "extra_arguments": ["--extra-vars=\"silo={{user `silo`}}\"", "-vv"]
    }
  ]
}

Here's the top (relevant) portion of my Ansible YAML file:

---
- hosts: all
  sudo: yes
  gather_facts: no
 
  tasks:
    - name: Show silo name
      debug: var=silo

    - name: Show silo name (2)
      debug: msg="Silo is {{ silo }}."

When I run Packer, this is the output:

$ packer build server.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Inspecting the source AMI...
==> amazon-ebs: Creating temporary keypair: packer 573fb51b-d86f-56a3-ee2f-453b9a2af8a2
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing access to port 22 the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
    amazon-ebs: Instance ID: i-9ede3445
==> amazon-ebs: Waiting for instance (i-9ede3445) to become ready...
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with Ansible...
==> amazon-ebs: SSH proxy: serving on 127.0.0.1:53000
==> amazon-ebs: Executing Ansible: ansible-playbook /Users/dwelch/src/automation/server.yml -i /var/folders/t7/lp4r7jb50m769y7z0bs5hf_80000gn/T/packer-provisioner-ansible769780155 --private-key /var/folders/t7/lp4r7jb50m769y7z0bs5hf_80000gn/T/ansible-key773453516 --extra-vars="silo=staging" -vv
    amazon-ebs: No config file found; using defaults
    amazon-ebs: [DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and
    amazon-ebs: make sure become_method is 'sudo' (default).
    amazon-ebs: This feature will be removed in a
    amazon-ebs: future release. Deprecation warnings can be disabled by setting
    amazon-ebs: deprecation_warnings=False in ansible.cfg.
    amazon-ebs:
    amazon-ebs: PLAYBOOK: server.yml *****************************************************
    amazon-ebs: 1 plays in /Users/dwelch/src/automation/server.yml
    amazon-ebs:
    amazon-ebs: PLAY [all] *********************************************************************
    amazon-ebs:
    amazon-ebs: TASK [Show silo name] **********************************************************
    amazon-ebs: task path: /Users/dwelch/src/automation/server.yml:15
    amazon-ebs: ok: [default] => {
    amazon-ebs:     "silo": "VARIABLE IS NOT DEFINED!"
    amazon-ebs: }
    amazon-ebs:
    amazon-ebs: TASK [Show silo name (2)] ******************************************************
    amazon-ebs: task path: /Users/dwelch/src/automation/server.yml:19
    amazon-ebs: fatal: [default]: FAILED! => {"failed": true, "msg": "'silo' is undefined"}
    amazon-ebs:
    amazon-ebs: NO MORE HOSTS LEFT *************************************************************
    amazon-ebs: to retry, use: --limit @/Users/dwelch/src/automation/server.retry
    amazon-ebs:
    amazon-ebs: PLAY RECAP *********************************************************************
    amazon-ebs: default                    : ok=1    changed=0    unreachable=0    failed=1
    amazon-ebs:
==> amazon-ebs: shutting down the SSH proxy
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: No AMIs to cleanup
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Error executing Ansible: Non-zero exit status: exit status 2


What I have tried:

- Passing in extra vars using -e
- Single quotes, double quotes, etc. around silo=staging 
- Passing in extra vars using the JSON syntax option
- Passing in via environmental vars (causes Ansible to try to connect to localhost rather than the instance on AWS)
- Using a single string value instead of an array for the extra_arguments
- Running against a test script:

---
- hosts: all
  gather_facts: no
  tasks:
    - name: Test
      debug: var=test

ansible-playbook test.yml -vvv -e 'test=foobar'

This works without issue.

Any help is much appreciated!

-Don


ai...@vcnc.co.kr

unread,
Jul 10, 2016, 11:02:08 PM7/10/16
to Packer
Instead of 

"extra_arguments": ["--extra-vars=\"silo={{user `silo`}}\"", "-vv"]

you can use

"extra_arguments": ["-e", "silo={{user `silo`}}", "-vv"]
Reply all
Reply to author
Forward
0 new messages