hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.06k stars 3.32k forks source link

Ansible playbook become: true not working when using with packer #13040

Closed jasonsun9406 closed 3 months ago

jasonsun9406 commented 3 months ago

Overview of the Issue

We are running a simple ansible playbook with ansible provisioner. When running the playbook without packer, the ansible playbook run just fine, but when running with packer, the ansible provisioner keep failing with the error message of msg: This command has to be run under the root user.

After some investigation, the ansible playbook trigger by packer doesn't seems to elevate to root even defining become: true. And we don't seems to be able to make ansible playbook to run as root user. Any help would be appreciated.

Here is the the simple ansible playbook I am trying to run

- name: 'Provision Image'
  become: true
  hosts: all
  tasks:
    - name: install Apache
      package:
        name: 'httpd'
        state: present

Here is the build block in the packer hcl file

build {
  sources = ["source.azure-arm.rhel"]

  provisioner "ansible" {
    playbook_file = "playbook.yml"
  }
...
}

Reproduction Steps

Using packer to provision a Red Hat image in Azure, and use the ansible provisioner to run the playbook in the overview

Plugin and Packer version

Packer version: v1.11.0 Ansible version: v2.10.7 Ansible provisioner version: v1.1.1

Operating system and Environment details

Source server (server runs packer command): Ubuntu Remote server: Red Hat

github-actions[bot] commented 3 months ago
Hi 👋 thanks for reaching out.

For general questions we recommend reaching out to the [community forum](https://discuss.hashicorp.com/c/packer) for greater visibility.
As the GitHub issue tracker is only watched by a small subset of maintainers and is really reserved for bugs and enhancements, you'll have a better chance of finding someone who can help you in the forum.
We'll mark this issue as needs-reply to help inform maintainers that this question is awaiting a response.
If no activity is taken on this question within 30 days it will be automatically closed.

If you find the forum to be more helpful or if you've found the answer to your question elsewhere please feel free to post a response and close the issue.
tenthirtyam commented 3 months ago

I do this in my project with:

build {
  sources = ["source.vsphere-iso.linux-rhel"]

  provisioner "ansible" {
    user                   = var.build_username
    galaxy_file            = "${path.cwd}/ansible/linux-requirements.yml"
    galaxy_force_with_deps = true
    playbook_file          = "${path.cwd}/ansible/linux-playbook.yml"
    roles_path             = "${path.cwd}/ansible/roles"
    ansible_env_vars = [
      "ANSIBLE_CONFIG=${path.cwd}/ansible/ansible.cfg",
      "ANSIBLE_PYTHON_INTERPRETER=/usr/libexec/platform-python"
    ]
    extra_arguments = [
      "--extra-vars", "display_skipped_hosts=false",
      "--extra-vars", "build_username=${var.build_username}",
      "--extra-vars", "build_key='${var.build_key}'",
      "--extra-vars", "ansible_username=${var.ansible_username}",
      "--extra-vars", "ansible_key='${var.ansible_key}'",
      "--extra-vars", "enable_cloudinit=${var.vm_guest_os_cloudinit}",
    ]
  }
---
- become: true
  become_method: sudo
  debugger: never
  gather_facts: true
  hosts: all
  roles:
    - base
    - users
    - configure
    - clean
lbajolet-hashicorp commented 3 months ago

Duplicate of https://github.com/hashicorp/packer-plugin-ansible/issues/190.

Since this is about the ansible plugin, I'll close this now, the discussion can continue on the linked issue.

github-actions[bot] commented 2 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.