ansible (remote) provisioner to "synchronize" files with packer-provided host

612 views
Skip to first unread message

Nirav Radia

unread,
Jul 14, 2016, 2:05:13 PM7/14/16
to Packer

My use-case is to run a playbook which installs Oracle JDK, GIT etc and "synchronize" a folder (say /home/ubuntu/abc) lying on my ansible-controller with ansible-host (at /tmp/abc).


Reading the documentation of ansible-remote, I thought using ansible provisioner would be good. But when I did that, to my surprise, the playbook was synchronizing the folder from /home/ubuntu/abc with /tmp/abc on the SAME MACHINE where I am running packer.


Is it possible that I can use machine running PACKER as ansible controller and use machine provisioned by packer as host?


Here is my packer.json:

{



"builders": [

{

"type": "amazon-ebs",

"access_key": "<<my_key_id>>",

"secret_key": "<<my_secret_key>>",

"ami_name": "CentOS 7 AMI by packer - {{timestamp}}",

"ami_description": "AMI created using packer",

"region": "us-west-2",

"source_ami": "ami-d2c924b2",

"instance_type": "t2.micro",

"ssh_username": "centos",

"user_data_file": "disable_tty.sh",

"ami_name": "packer-example-{{timestamp}}",

"tags": {

"Name": "web-nginx"

}

}

],

"provisioners": [

{

"type": "ansible",

"playbook_file": "ansible/install-binaries.yml",

"user": "centos",

"extra_arguments": ["-vvvv"],

"sftp_command": "/usr/libexec/openssh/sftp-server"

}

]

}


install-binaries.yml


---



- name: Install JAVA 1.8 if needed

  hosts
: all

  become
: true

  roles
:

   
- williamyeh.oracle-java




- name: Install git

  hosts
: all

  become
: true

  roles
:

   
- davidkarban.git

   

- name: Install rsync

  hosts
: all

  become
: true

  roles
:

   
- kbrebanov.rsync




- name: Copy my directory

  hosts
: all

  become
: true

  roles
:


   
- ABC.myrole




tasks/main.yml of role ABC.myrole


---


- name: Remove installation if exists

  file
: path="{{deploy_dir}}" state="absent"




- name: Synchronize installation

  synchronize
: src="{{src_dir}}/" dest="{{deploy_dir}}"




- name: Make shell scripts executable

  shell
: "find ./ -name '*.sh' -exec chmod +x {} \\;"

  args
:

    chdir
: "{{deploy_dir}}"



disable_tty.sh

#!/bin/bash
sed
-i '/Defaults \+requiretty/s/^/#/' /etc/sudoers



The variable values are:

deploy_dir: "/tmp/myDirectory"

src_dir: ".~/ansible/myDirectory" 


Directory structure:

/home/ubuntu/

-- packer.json

-- disable_tty.sh (Need this for "become: true" in playbook on CentOS 7 image which I am using)

-- ansible

   -- roles (all the roles are inside this)

   -- install-binaries.yml

-- myDirectory


The task "Synchronize installation" takes too long (waited till 15 minutes and then stopped it !!!) and meanwhile, I observed there is myDirectory created in /tmp of the PACKER HOST (of course I can't check on host which PACKER is providing the temp instance it is in creation state)
The size of myDirectory is more than 550 MB and it contains a LOT of little files and binaries.

Machine I am running packer is Ubuntu 14.04
Source AMI is Centos 7 from its official website.

Matzuba

unread,
Jan 17, 2017, 1:20:13 AM1/17/17
to Packer
Hi there

Did you get this working at all?

I am trying to figure how to get packer to work with ansible roles i have created.  I dont want to use one playbook file.

cheers

Nirav Radia

unread,
Jan 17, 2017, 2:39:21 AM1/17/17
to Packer
Hi,

I gave up after a few tries on synchronizing the whole folder and created gzip of it and copied it using ansible copy module and extracted on remote machine at desired location.

Regards,
Nirav

Matzuba

unread,
Jan 17, 2017, 9:16:51 PM1/17/17
to Packer
Hi Nirav and thanks for the reply.

Sorry, i wasn't clear.  i am interested in your role setup with Packer. 

Do roles work exactly the same as if i was just applying to a running server?  Did you have to do anything different or factor any changes in files or directory layout?

My goal is to have a role that can be run on any server, be that a running instance or with packer AMI image. I only want to maintain the compliance role in one repo with all the rest of our ansible code.  I am just not sure if this will work with Packer and that there will be subtle differences.  Eg, host will be all, 

I'm looking to something like:

.
├── ansible.cfg
├── environment
  └── packerbuild
      ├── group_vars
      │─  hosts
      └── host_vars
          └──
  └── dev
      ├── group_vars
         
      ├── hosts
      └── host_vars
         
├── hostkeys
  └── hostkey1
├── playbooks
  ├── build.yml
├── roles
   
  └── compliance
      ├── defaults
        └── main.yml
      ├── files
        └── file1.sh
      ├── handlers
        └── main.yml
      ├── meta
        └── main.yml
      ├── README.md
      ├── tasks
        ├── task1.yml
|       |   |- main.yml
               
        └── test.yml
      ├── templates
        ├── template1.j2.
     
      ├── tests
        ├── inventory
        └── test.yml
      └── vars
          └── main.yml


I have a build playbook under ansible/playbooks which calls the compliance role.  This role has multiple include statements in the role/tasks/mainyml
This works fine to apply to a running server from my control node but  it seems that the playbook calling the role cant be in ansible/playbooks but must be relative to the role so i had to move this one level up.

cat packer-role/build.yml
---

- name:  Build Role
  hosts: all
  roles:
    - role: compliance



Would you mind sharing any guidance you have with using roles and ansible-remote provisioner?   
Do you have a separate ansible code base for packer?

Cheers

Mat



Reply all
Reply to author
Forward
0 new messages