3

I'm trying to provision Ansible playbook using packer on Windows AMI.
That's my packer template:

{
  "variables": {
    "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
    "aws_region": "{{env `AWS_DEFAULT_REGION`}}",
    "aws_source_ami": "*****",
    "ssh_username": "{{env `AWS_AMI_USERNAME`}}",
    "aws_instance_type": "m1.medium",
    "name": "windows2012-...",
    "packer_dir": "/opt/packer",
    "home": "{{env `HOME`}}"
},
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "{{user `aws_region`}}",
    "source_ami": "{{user `aws_source_ami`}}",
    "ami_regions": ["{{user `aws_region`}}"],
    "instance_type": "{{user `aws_instance_type`}}",
    "communicator": "winrm",
    "winrm_username": "Administrator",
    "winrm_use_ssl": true,
    "winrm_insecure": true,
    "winrm_timeout": "12h",
    "user_data_file": "scripts/userdata_setup.ps1",
    "ami_name": "{{user `name`}}-ami",
    "ami_description": "{{user `name`}}-ami",
    "associate_public_ip_address": true,
    "launch_block_device_mappings": [{
      "device_name": "/dev/xvda",
      "volume_type": "gp2",
      "volume_size": 50,
      "delete_on_termination": true
  }],
    "tags": {
      "artifact": "{{user `name`}}"
    }
  }],
  "provisioners": [
    {
      "type": "powershell",
      "script": "scripts/ConfigureRemotingForAnsible.ps1"
    },
    {
      "type": "ansible",
      "playbook_file": "/path/to/playbook_file.yml",
      "extra_arguments" : [
        "--extra-vars", "ansible_user=Administrator ansible_connection=winrm ansible_winrm_server_cert_validation=ignore"
      ]
    }
  ]
}

When I'm running packer build my_template.json
I'm getting the following error while the Ansible provisioner is running:

amazon-ebs: TASK [Gathering Facts] *********************************************************
amazon-ebs: fatal: [default]: UNREACHABLE! => {"changed": false, "msg":
            "ssl: HTTPSConnectionPool(host='127.0.0.1', port=5986): Max retries
            exceeded with url: /wsman (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x102ba6f90>: 
            Failed to establish a new connection: [Errno 61] Connection refused',))", "unreachable": true}

I guess I'm doing something wrong (why the host is 127.0.0.1?), but I haven't found any documentation about running ansible using packer on Windows in AMI.

Does anyone knows what am I doing wrong? How can I resolve my issue?


Best regards,
Adam

Adamba4
  • 181
  • 1
  • 2
  • 9
  • The traffic is proxied via packer. So Ansible runs on your local packer machine and connects to 127.0.0.1 (the packer executable) and which forwards the traffic to the instance. – Rickard von Essen Apr 24 '17 at 12:48

1 Answers1

0

You have to follow the instructions in the documentation Packer Documentation - Ansible: winrm communicator and use a custom connection plugin and use the following ansible provisioner args:

  "extra_arguments": [
    "--connection", "packer",
    "--extra-vars", "ansible_shell_type=powershell ansible_shell_executable=None"
  ]
Rickard von Essen
  • 4,110
  • 2
  • 23
  • 27
  • Thanks! Now I'm getting the following error: `amazon-ebs: fatal: [default]: FAILED! => {"failed": true, "msg": "failed to transfer file to 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1493043899.96-87816042318692\\setup.ps1':\n\n\n"} amazon-ebs: fatal: [default]: FAILED! => {"failed": true, "msg": "failed to transfer file to 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1493043899.96-87816042318692\\setup.ps1':\n\n\n"}` – Adamba4 Apr 24 '17 at 14:33
  • Hi @Adamba4, have you found a solution ? – elhostis May 04 '17 at 12:11
  • @elhostis try posting a new Q or on the Packer email list, see https://www.packer.io/community.html . – Rickard von Essen May 04 '17 at 12:22