title: Run ansible-playbook from code/command line connecting to docker container
body: <p>I am using this:</p>
  
  <p><a href="https://stackoverflow.com/questions/47600633/specify-docker-containers-in-etc-ansible-hosts-file">Specify docker containers in /etc/ansible/hosts file</a></p>
  
  <p>to run my ansible playbooks against a docker container.</p>
  
  <p>But is there any way to avoid having a physical <code>/etc/ansible/hosts</code> file with the information about the container? E.g. run it from code where this information can be configured?</p>
  
  <p>I looked at:</p>
  
  <p><a href="https://stackoverflow.com/questions/27590039/running-ansible-playbook-using-python-api">Running ansible-playbook using Python API</a></p>
  
  <p>but when looking at the answers I see variables pointing to physical files, e.g.:</p>
  
  <pre><code>inventory = Inventory(loader=loader, sources='/home/slotlocker/hosts2')
  playbook_path = '/home/slotlocker/ls.yml'
  </code></pre>
  
  <p>So not really sure why that is better than simply just calling from command line without using the Python ansible API.</p>