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}}"#!/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
Hi Nirav and thanks for the reply.
.
├── 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