在 Yandex 云中使用 Ansible 配置 Packer 失败

Packer provisioning with ansible fails in yandex cloud

我正在尝试在本地 CentOS 机器上的 yandex-cloud 中使用 Packer 创建图像。
Packer 使用 Ansible provisioner 并运行包含该角色的剧本。

Packer 模板:

{
    "variables": {
           "zone": "ru-central1-a",
           "instance_cores": "2"
       },
    "builders": [
       {
           "type": "yandex",
           "service_account_key_file": "{{user `service_account_key_file`}}",
           "folder_id": "{{user `folder_id`}}",
           "source_image_family": "{{user `source_image_family`}}",
           "image_name": "reddit-db-base",
           "image_family": "reddit-base",
           "ssh_username": "ubuntu",
           "platform_id": "standard-v1",
           "zone": "{{user `zone`}}",
           "instance_cores": "{{user `instance_cores`}}",
       "use_ipv4_nat" : "true"
       }
   ],
   "provisioners": [
       {
           "type": "ansible",
           "playbook_file": "../ansible/playbooks/packer_db.yml",
           "extra_arguments": ["--tags","install"],
           "ansible_env_vars": ["ANSIBLE_ROLES_PATH=../ansible/roles"]
       }
   ]
}

Ansible 剧本:

- name: Installing MongoDB
  hosts: all
  become: true
  roles: 
    - db

但是,在处理过程中出现以下错误:

...
==> yandex: Waiting for SSH to become available...
==> yandex: Connected to SSH!
==> yandex: Provisioning with Ansible...
    yandex: Setting up proxy adapter for Ansible....
==> yandex: Executing Ansible: ansible-playbook -e packer_build_name="yandex" -e packer_builder_type=yandex --ssh-extra-args '-o IdentitiesOnly=yes' --tags install -e ansible_ssh_private_key_file=/tmp/ansible-key647683404 -i /tmp/packer-provisioner-ansible030511675 /home/ansible/playbooks/packer_db.yml
    yandex:
    yandex: PLAY [Installing MongoDB] *******************************************************
    yandex:
    yandex: TASK [Gathering Facts] *********************************************************
    yandex: fatal: [default]: UNREACHABLE! => {"changed": false, "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-1614119638.2707298-11423-111899549529332 `\" && echo ansible-tmp-1614119638.2707298-11423-111899549529332=\"` echo /root/.ansible/tmp/ansible-tmp-1614119638.2707298-11423-111899549529332 `\" ), exited with result 1", "unreachable": true}
    yandex:
    yandex: PLAY RECAP *********************************************************************
    yandex: default                    : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
...

我该如何解决这个问题?如有任何帮助,我们将不胜感激!

问题已通过在 Packer 模板的 provisioners 部分添加用户解决:

"provisioners": [
       {
           "type": "ansible",
           "user": "ubuntu",
           "playbook_file": "../ansible/playbooks/packer_db.yml",
           "extra_arguments": ["--tags","install"],
           "ansible_env_vars": ["ANSIBLE_ROLES_PATH=../ansible/roles"]
       }
]