ansible sudo: sorry, you must have a tty to run sudo

I was able to fix this by setting the transport = paramiko configuration in ansible.cfg.


I have found the following solutions for myself:
1. Change requiretty in /etc/sudoers with sed run playbooks and change it back.

 "#!/bin/bash\n", "\n", "  
 echo 'Installing Git'\n"," 
 yum --nogpgcheck -y install git ansible htop nano wget\n",  
 "wget https://s3.eu-central-1.amazonaws.com/xx/ansible -O /root/.ssh/id_rsa\n",  
 "chmod 600 /root/.ssh/id_rsa\n",   
  "ssh-keyscan 172.31.9.231 >> /root/.ssh/known_hosts\n",   
  "git clone [email protected]:somerepo/dev.git /root/dev\n",   
  "sed -i 's/Defaults    requiretty/Defaults    !requiretty/g' /etc/sudoers\n", 
  "\n", 
  "ansible-playbook /root/dev/env/ansible/uk.yml\n",   
  "\n",   
  "sed -i 's/Defaults    !requiretty/Defaults    requiretty/g' /etc/sudoers\n"   

OR 2. In ansible playbook specify variable:

 - name: Setup 
 hosts: 127.0.0.1
 connection: local
 sudo: {{ require_sudo }}
 roles:
    - generic

Run in AWS Cloud Formation template would be

   "ansible-playbook -e require_sudo=False /root/dev/env/ansible/uk.yml\n"  

And for Vagrant in ansible.cfg it can be specified

  require_sudo=True
  1. Also in CF template may identify who is running and the pass variable

    ansible-playbook -e$(id -u |egrep '^0$' > /dev/null && require_sudo=False || require_sudo=True; echo "require_sudo=$require_sudo") /apps/ansible/uk.yml