title: How to run ansible script inside a docker container
body: <p>I am using docker for testing my playbooks.</p>
  
  <p>I created a container now when i am running below command inside container its giving me below error</p>
  
  <p><strong>ansible-playbook  jenkins.yml</strong></p>
  
  <p><strong>Error:-</strong></p>
  
  <pre><code>[root@db1e9105692d jenkins-playbook]# ansible-playbook  jenkins.yml -k -vvv
  SSH password:
  
  PLAY [localhost] **************************************************************
  
  GATHERING FACTS ***************************************************************
  &lt;localhost&gt; ESTABLISH CONNECTION FOR USER: root
  &lt;localhost&gt; REMOTE_MODULE setup
  &lt;localhost&gt; EXEC sshpass -d4 ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o PubkeyAuthentication=no -o ConnectTimeout=10 localhost /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1454580537.38-114451000565344 &amp;&amp; echo $HOME/.ansible/tmp/ansible-tmp-1454580537.38-114451000565344'
  EXEC previous known host file not found for localhost
  fatal: [localhost] =&gt; SSH Error: ssh: connect to host localhost port 22: Connection refused
      while connecting to 127.0.0.1:22
  It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
  
  TASK: [jenkins | Include OS-Specific variables] *******************************
  &lt;localhost&gt; ESTABLISH CONNECTION FOR USER: root
  fatal: [localhost] =&gt; One or more undefined variables: 'ansible_os_family' is undefined
  
  FATAL: all hosts have already failed -- aborting
  
  PLAY RECAP ********************************************************************
             to retry, use: --limit @/root/jenkins.retry
  
  localhost                  : ok=0    changed=0    unreachable=2    failed=0
  </code></pre>
  
  <p>But if i run this command on host machine its running fine.Do i need to do anything so that connection do not gets refused on port 22.inside docker container</p>
  
  <p>Please do not consider below line as reason for error.Its just that ansible has executed few more lines before  throwing error. Actually its not able to run so thats why value of this variable is empty.</p>
  
  <pre><code>fatal: [localhost] =&gt; One or more undefined variables: 'ansible_os_family' is undefined
  </code></pre>