thinkingmonster
thinkingmonster

Reputation: 5413

How to run ansible script inside a docker container

I am using docker for testing my playbooks.

I created a container now when i am running below command inside container its giving me below error

ansible-playbook jenkins.yml

Error:-

[root@db1e9105692d jenkins-playbook]# ansible-playbook  jenkins.yml -k -vvv
SSH password:

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
<localhost> ESTABLISH CONNECTION FOR USER: root
<localhost> REMOTE_MODULE setup
<localhost> 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 && echo $HOME/.ansible/tmp/ansible-tmp-1454580537.38-114451000565344'
EXEC previous known host file not found for localhost
fatal: [localhost] => 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] *******************************
<localhost> ESTABLISH CONNECTION FOR USER: root
fatal: [localhost] => 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

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

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.

fatal: [localhost] => One or more undefined variables: 'ansible_os_family' is undefined

Upvotes: 0

Views: 4958

Answers (2)

ferdy
ferdy

Reputation: 7716

In your container start your playbook locally:

$ ansible-playbook jenkins.yml -c local -k -vvv

Upvotes: 4

udondan
udondan

Reputation: 59969

Do you have connection=local defined for localhost? It's trying to connect via ssh, which can not work because you probably do not have sshd running in your container.

Upvotes: 0

Related Questions