1

I'm using Packer/Ansible to create AMIs via my GitLab CI/CD pipeline, therefore I'm using the Packer Docker image and installing ansible locally on the GitLab runner using apk update && apk add --no-cache ansible.

Now, when I run packer build build.json since I'm using the ansible provisioner, Packer will create an EC2 instance on a public subnet to which the ansible executable will SSH into successfully.

The issue is when running the playbook, it will try to create a temporary directory, and the playbook will fail then, here are the logs:

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

==> amazon-ebs: Prevalidating any provided VPC information
==> amazon-ebs: Prevalidating AMI Name: wtg-ami
    amazon-ebs: Found Image ID: ami-0aef57767f5404a3c
    amazon-ebs: Found Subnet ID: subnet-00518d347fd748c70
==> amazon-ebs: Creating temporary keypair: packer_5fe47275-27ce-105a-f5bf-d26f8fd8e85d
==> amazon-ebs: Creating temporary security group for this instance: packer_5fe4727a-3e05-da8b-73e0-c2ad53e0b75b
==> amazon-ebs: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-0f02f0d7f1cc595a4
==> amazon-ebs: Waiting for instance (i-0f02f0d7f1cc595a4) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 34.240.9.95
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with Ansible...
    amazon-ebs: Setting up proxy adapter for Ansible....
==> amazon-ebs: Executing Ansible: ansible-playbook -e packer_build_name="amazon-ebs" -e packer_builder_type=amazon-ebs --ssh-extra-args '-o IdentitiesOnly=yes' -vvv -e ansible_ssh_private_key_file=/tmp/ansible-key754572492 -i /tmp/packer-provisioner-ansible109011387 /builds/FaresKi/what-the-git/playbook.yml
    amazon-ebs: ansible-playbook 2.9.14
    amazon-ebs:   config file = None
    amazon-ebs:   configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    amazon-ebs:   ansible python module location = /usr/lib/python3.8/site-packages/ansible
    amazon-ebs:   executable location = /usr/bin/ansible-playbook
    amazon-ebs:   python version = 3.8.5 (default, Jul 20 2020, 23:11:29) [GCC 9.3.0]
    amazon-ebs: No config file found; using defaults
    amazon-ebs: host_list declined parsing /tmp/packer-provisioner-ansible109011387 as it did not pass its verify_file() method
    amazon-ebs: script declined parsing /tmp/packer-provisioner-ansible109011387 as it did not pass its verify_file() method
    amazon-ebs: auto declined parsing /tmp/packer-provisioner-ansible109011387 as it did not pass its verify_file() method
    amazon-ebs: Parsed /tmp/packer-provisioner-ansible109011387 inventory source with ini plugin
    amazon-ebs:
    amazon-ebs: PLAYBOOK: playbook.yml *********************************************************
    amazon-ebs: 1 plays in /builds/FaresKi/what-the-git/playbook.yml
    amazon-ebs:
    amazon-ebs: PLAY [all] *********************************************************************
    amazon-ebs:
    amazon-ebs: TASK [Gathering Facts] *********************************************************
    amazon-ebs: task path: /builds/FaresKi/what-the-git/playbook.yml:2
    amazon-ebs: <127.0.0.1> ESTABLISH PARAMIKO SSH CONNECTION FOR USER: root on PORT 32917 TO 127.0.0.1
    amazon-ebs: <127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'
    amazon-ebs: <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1608807068.967218-74-245195742932122 `" && echo ansible-tmp-1608807068.967218-74-245195742932122="` echo /root/.ansible/tmp/ansible-tmp-1608807068.967218-74-245195742932122 `" ) && sleep 0'
    amazon-ebs: fatal: [default]: UNREACHABLE! => {
    amazon-ebs:     "changed": false,
    amazon-ebs:     "msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo /root/.ansible/tmp `\"&& mkdir \"` echo /root/.ansible/tmp/ansible-tmp-1608807068.967218-74-245195742932122 `\" && echo ansible-tmp-1608807068.967218-74-245195742932122=\"` echo /root/.ansible/tmp/ansible-tmp-1608807068.967218-74-245195742932122 `\" ), exited with result 1",
    amazon-ebs:     "unreachable": true
    amazon-ebs: }

I really don't understand what's wrong, maybe you guys could see something that I don't... TIA

Fares
  • 893
  • 1
  • 11
  • 24
  • 2
    Have you tried doing what the error message advises to do? (ie. setting temporary directory root under /tmp?) – guido Dec 24 '20 at 11:53
  • I tried, didn't change anything... Decided to go with shell provisioning – Fares Dec 24 '20 at 13:20
  • Please look at this thread>>https://stackoverflow.com/questions/58188715/packer-provisioning-by-ansible-fails-in-aws-codebuild this solved my problem – Biplab Jan 25 '21 at 21:58

0 Answers0