Is it possible to reference the current ssh user as a var with Ansible as a packer provisioner?

Although Ansible with packer always connects as the default user, my scripts have a need to refer to that user as a var, which is different for the multiple builds. Is it possible to pass through the current user as a variable? eg:

  provisioner "ansible" {
    extra_arguments = [
      "-v",
      "--extra-vars",
      "variable_host=default variable_connect_as_user={{user `USER`}} variable_user={{user `USER`}} variable_become_user={{user `USER`}} delegate_host=localhost",
      "--skip-tags",
      "user_access"
    ]
    playbook_file    = "./ansible/aws_cli_ec2_install.yaml"
    collections_path = "./ansible/collections"
    roles_path       = "./ansible/roles"
    ansible_env_vars = ["ANSIBLE_CONFIG=ansible/ansible.cfg"]
    galaxy_file      = "./requirements.yml"
    only             = ["amazon-ebs.deadline-db-ubuntu18-ami"]
  }

If I understand correctly, you need to reference the user which is currently connected to the instance you are provisioning, within the playbook, right?

This is contained in an internal Ansible variable : ansible_user:

’ ansible_user’
The user Ansible ‘logs in’ as.

You should be able to look it up during the execution of a task by passing {{ ansible_user }}.

1 Like

Thanks @brucellino1 I don’t know why I had trouble finding something like that!

1 Like

I have a related extension to this old question…

My ansible is pretty rusty, but I’m repeating myself in this packer code when it comes to specifying different users based on the ami target. Is it possible to handle variables dynamically based on the target image? in each case the user to connect as is different between rocky and ec2-user. Does packer itself support dynamic vars based on the target ami?

  provisioner "ansible" { # Add user deployuser
    playbook_file = "./ansible/newuser.yaml"
    user          = "rocky"
    extra_arguments = [
      "-v",
      "--extra-vars",
      "variable_user=deployuser sudo=true passwordless_sudo=true add_to_group_syscontrol=true variable_connect_as_user=rocky variable_uid=${local.deployuser_uid} syscontrol_gid=${local.syscontrol_gid} variable_host=default delegate_host=localhost"
    ]
    collections_path = "./ansible/collections"
    roles_path       = "./ansible/roles"
    ansible_env_vars = ["ANSIBLE_CONFIG=ansible/ansible.cfg"]
    galaxy_file      = "./requirements.yml"
    only = [
      "amazon-ebs.rocky8-rendernode-ami",
    ]
  }

  provisioner "ansible" { # Add user deployuser
    playbook_file = "./ansible/newuser.yaml"
    user          = "ec2-user"
    extra_arguments = [
      "-v",
      "--extra-vars",
      "variable_user=deployuser sudo=true passwordless_sudo=true add_to_group_syscontrol=true variable_connect_as_user=ec2-user variable_uid=${local.deployuser_uid} syscontrol_gid=${local.syscontrol_gid} variable_host=default delegate_host=localhost"
    ]
    collections_path = "./ansible/collections"
    roles_path       = "./ansible/roles"
    ansible_env_vars = ["ANSIBLE_CONFIG=ansible/ansible.cfg"]
    galaxy_file      = "./requirements.yml"
    only = [
      "amazon-ebs.amznlnx2023-rendernode-ami",
    ]
  }