How to enable passwordless sudo programmatically to ubuntu user without access to root

I am doing a fresh install of Ubuntu 18.04 via automated methods which runs a script under the ubuntu user programmatically. However when the script tries to run sudo it prompts for the users password. I know I can prevent prompting for the ubuntu password on sudo by doing:

echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers > /dev/null 

However, this fails as I need sudo without password to update /etc/sudoers programmatically.

How can I accomplish this? I know on Amazon EC2 the Ubuntu AMI uses cloudinit to automatically enable passwordless sudo. It seems like cloudinit is running, how can I configure it to mirror what Amazon EC2 Ubuntu AMI does?