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