Ok - take two. After a lot of poking and prodding and head banging I think 
I've identified the issue as the provisioner not incorporating my existing 
group_vars definition.

I put a debug statement into the provisioner.go file which dumps the 
generated inventory file and it looks like this:
[consul_instances] 
2018/03/20 23:16:52 packer: default ansible_host=127.0.0.1 ansible_user=
[email protected] ansible_port=37147

So the container is being assigned to a group.

What doesn't seem to be happening is that despite setting the 
"inventory_directory" to the directory containing my host file and my 
group_vars directory, the provisioner is not grabbing the variables defines 
in my group_vars/consul_instances.yml file.

Here's my test setup:

template.json:
{
  "builders": [
    {
      "type": "lxd",
      "name": "consulserver",
      "image": "AWTAlpine37",
      "output_image": "consulserver",
      "publish_properties": {
      "description": "Consul Server"
      }
    }
  ],
  "provisioners": [
  {
    "type": "shell",
    "inline": [ "sleep 10; apk update && apk add python2" ]
  },
  {
    "type": "ansible",
    "groups": ["consul_instances"],
    "ansible_env_vars": [ 
"ANSIBLE_CONFIG=/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/ansible.cfg"
 
],
    "inventory_directory": 
"/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/inventories/prod",
    "playbook_file": 
"/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/playbooks/domain-server.yml",
    "extra_arguments": [ "-c", "lxd", "-i", "packer-consulserver,",  "-l", 
"packer-consulserver" ]
  }
  ]
}

Playbook:

---
- hosts: all


  tasks:
    - name: Play hosts
      debug: msg="play_hosts={{play_hosts}}"
      run_once: true


    - name: Dump consul_instance group
      debug: msg="consul instances {{ groups['consul_instances'] | 
to_nice_yaml }}"


    - name: Dump workstations group
      debug: var=groups['workstations']


    - name: Dump consul_instances group var
      debug: var=consul_node_role


Output from packer run:
 consulserver: Provisioning with Ansible...
==> consulserver: Executing Ansible: ansible-playbook --extra-vars 
packer_build_name=consulserver packer_builder_type=lxd -i 
/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/inventories/prod/packer-provisioner-ansible074578073
 
/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/playbooks/domain-server.yml
 
-e ansible_ssh_private_key_file=/tmp/ansible-key628749218 -c lxd -i 
packer-consulserver, -l packer-consulserver --extra-vars 
remote_addr=packer-consulserver
    consulserver:
    consulserver: PLAY [all] 
*********************************************************************
    consulserver:
    consulserver: TASK [Gathering Facts] 
*********************************************************
    consulserver:  [WARNING]: lxd does not support remote_user, using 
container default: root
    consulserver: ok: [packer-consulserver]
    consulserver:
    consulserver: TASK [Play hosts] 
**************************************************************
    consulserver:  [WARNING]: lxd does not support remote_user, using 
container default: root
    consulserver: ok: [packer-consulserver] => {
    consulserver:     "msg": "play_hosts=[u'packer-consulserver']"
    consulserver: }
    consulserver:
    consulserver: TASK [Dump consul_instance group] 
**********************************************
    consulserver:  [WARNING]: lxd does not support remote_user, using 
container default: root
    consulserver: ok: [packer-consulserver] => {
    consulserver:     "msg": "consul instances - default\n"
    consulserver: }
    consulserver:
    consulserver: TASK [Dump workstations group] 
*************************************************
    consulserver:  [WARNING]: lxd does not support remote_user, using 
container default: root
    consulserver: ok: [packer-consulserver] => {
    consulserver:     "groups['workstations']": "VARIABLE IS NOT DEFINED!"
    consulserver: }
    consulserver:
    consulserver: TASK [Dump consul_instances group var] 
*****************************************
    consulserver:  [WARNING]: lxd does not support remote_user, using 
container default: root
    consulserver: ok: [packer-consulserver] => {
    consulserver:     "consul_node_role": "VARIABLE IS NOT DEFINED!"
    consulserver: }
    consulserver:
    consulserver: PLAY RECAP 
*********************************************************************
    consulserver: packer-consulserver        : ok=5    changed=0    
unreachable=0    failed=0
    consulserver:


consul_instances.yml group-var file:
---
consul_node_role: server
consul_bootstrap_expect: true


Directory structure of inventory_directory:
/home/FUZZBUTT/snesbitt/projects/ansible/fuzzbutt.awt_ansible/inventories/
prod
├── group_vars
│   ├── all.yml
│   ├── consul_hosts.yml
│   ├── consul_instances.yml
│   └── consul_servers.yml
├── hosts
└── host_vars

God I hope this is a bug and not something obviously stupid on my part ;-)

-stefve

-- 
This mailing list is governed under the HashiCorp Community Guidelines - 
https://www.hashicorp.com/community-guidelines.html. Behavior in violation of 
those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
--- 
You received this message because you are subscribed to the Google Groups 
"Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/packer-tool/e9861209-6750-47c4-8165-65203af0a2e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to