0

I am new to Packer + Ansible combination example. Below is my provisioner which is calling ansible yml file using Azure builder,

"provisioners": [
        {
            "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'",
            "inline": [
                "sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm",
                "sudo yum -y install ansible"
            ],
            "inline_shebang": "/bin/sh -x",
            "type": "shell"
        },
        {
            "type": "ansible-local",
            "playbook_file": "/tmp/app.yml"
        }
    ]

In my app.yml, I want to copy my script to temporary vm (pkr* ) that gets created at the time of packer build run. So /tmp/script.sh will be executed. However I am not getting exact module to be used to copy file from local(/tmp/script.sh) to packer builder vm.

app.yml

- hosts: localhost
  tasks:
  - name: copy script from local to vm
    ansible.builtin.template:
      src: /tmp/script.sh
      dest: /tmp/
  - name: validate copied files
    command: ls /tmp
  - copy:
      src: /tmp/script.sh
      dest: /tmp/
  - command: ls /tmp/
  - script: /tmp/script.sh

Error :

azure-arm: TASK [copy script from local to vm] ****************************************
    azure-arm: fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Could not find or access '/tmp/script.sh' on the Ansible Controller.\nIf you are usin
g a module and expect the file to exist on the remote, see the remote_src option"}

I have tried both copy and ansible.builtin.template modules, but no luck.

Uday Kiran
  • 487
  • 2
  • 9
  • 29
  • the error is clear: your file script.sh is not found...your file exists on ansible server master? – Frenchy May 30 '22 at 14:12
  • @Frenchy But it is present in my local machine. However, when packer build creates new environment for building the image. It is checking in that new VM ( usually starts with pkr ) which is not present. Now I want to copy it before ansible module run. – Uday Kiran May 30 '22 at 14:16
  • You can use below code, ``` { "type": "file", "source": "/your/path", "destination": "/tmp/" } { "type": "ansible-local", "playbook_file": "app.yml" } ``` – Uday Kiran Mar 15 '23 at 19:11

0 Answers0