# Get rid of "previous known host file not found"

**URL:** <https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427>\
**Category:** Ansible Project\
**Created:** [January 26, 2014, 1:14pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427 "2014-01-26T13:14:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [January 26, 2014, 1:14pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/1 "2014-01-26T13:14:28Z")

</div>

I get 2-4 “previous known host file not found” warning on each task execution.  
Like  
TASK: [configure .bashrc for me] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
previous known host file not found  
previous known host file not found  
previous known host file not found  
previous known host file not found  
ok: [test1]  
Naturally, ~/.ssh/known\_hosts exists and is readable.  
I think it happened at some point but for the life of me I can’t find any related configuration option. Just how do I fix it?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 27, 2014, 1:35pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/2 "2014-01-27T13:35:56Z")

</div>

What version of Ansible are you using?

Also, what connection type is being used (did you specify -c ssh or -c paramiko? If not, what’s your OS you are running Ansible from?)

Can you show your current user id from bash and also “ls -l” on the known\_hosts file?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [January 27, 2014, 2:14pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/3 "2014-01-27T14:14:51Z")

</div>

Are you perhaps running ansible locally through sudo?

# sudo ansible …

I had this issue some time ago, and a patch wa accepted to look at the sudo user’s home dir, nog the calling user, which solved that.  
I still get this for jobs that run from cron though (no sudo involved here), but didn’ look at it further so far.

recent ansible 1.5 devel, running on default -c ssh

Serge

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 2, 2014, 3:34pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/4 "2014-02-02T15:34:17Z")

</div>

I can no longer reproduce that.

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 2, 2014, 4:39pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/5 "2014-02-02T16:39:31Z")

</div>

Hmm, now appeared again.

$ ansible --version  
ansible 1.4.3  
$ cat /etc/redhat-release  
CentOS release 6.5 (Final)  
$ rpm -q openssh-clients  
openssh-clients-6.4p1-1.el6.2.x86\_64  
$ id  
uid=500(magi) gid=500(magi) groups=500(magi),10(wheel)  
$ls -l ~/.ssh  
total 28  
-rw------- 1 magi magi 789 Jan 12 18:47 authorized\_keys  
-rw------- 1 magi magi 1675 Apr 17 2013 id\_rsa  
-rw-r–r-- 1 magi magi 395 Jan 12 13:17 id\_rsa.pub  
-rw-r–r-- 1 magi magi 13375 Feb 2 14:57 known\_hosts

I don’t use sudo. The command is run like this:  
time ansible-playbook $task -e “hosts=$host” -i $inventory -s

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 7, 2014, 9:13pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/6 "2014-02-07T21:13:56Z")

</div>

So what can I do to finally find the previous host file?

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [February 7, 2014, 11:39pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/7 "2014-02-07T23:39:21Z")

</div>

If you can reliably repeatably reproduce this, do the run with -vvvv to make sure the ssh args match up with what you think they should, with respect to where the known hosts file should be, etc...

-jlk

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 8, 2014, 10:28am UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/8 "2014-02-08T10:28:12Z")

</div>

How do I know what they should be?

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 10, 2014, 10:13pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/9 "2014-02-10T22:13:21Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [February 10, 2014, 10:54pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/10 "2014-02-10T22:54:19Z")

</div>

Well, you'd want to look at what ansible is using, and try using them yourself manually and see if the problem persists.

This doesn't appear to be a common problem, I really would say it's something specific to your environment and usage.

-jlk

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 13, 2014, 8:37pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/11 "2014-02-13T20:37:50Z")

</div>

Well, it doesn’t really matter to me whether the problem is specific or not.

Here’s how a verbose ansible connection looks:  
\<192.168.7.188\> ESTABLISH CONNECTION FOR USER: ilya  
\<192.168.7.188\> EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/home/ilya/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=17722’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=ilya’, ‘-o’, ‘ConnectTimeout=10’, ‘192.168.7.188’, “/bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1392322724.99-173609494693332 && chmod a+rx $HOME/.ansible/tmp/ansible-1392322724.99-173609494693332 && echo $HOME/.ansible/tmp/ansible-1392322724.99-173609494693332’”]  
previous known host file not found

Here are attempts to reproduce it with ssh  
$ ssh -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/home/ilya/.ansible/cp/ansible-ssh-%h-%p-%r -o Port=17722 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-micgssapi-keyexhostbasedpublickey -o PasswordAuthentication=no -o User=ilya -o ConnectTimeout=10 192.168.7.188 2\>&1 | grep previous  
$  
$ ssh -tt -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/home/ilya/.ansible/cp/ansible-ssh-%h-%p-%r -o Port=17722 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-micgssapi-keyexhostbasedpublickey -o PasswordAuthentication=no -o User=ilya -o ConnectTimeout=10 192.168.7.188  
Permission denied (publickey,gssapi-with-mic,password).  
$  
$ ssh -tt -o ControlMaster=auto -o ControlPersist=60s -o Port=17722 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-micgssapi-keyexhostbasedpublickey -o PasswordAuthentication=no -o User=ilya -o ConnectTimeout=10 192.168.7.188  
Permission denied (publickey,gssapi-with-mic,password).  
$  
$ssh -tt -o ControlMaster=auto -o ControlPersist=60s -o Port=17722 -o KbdInteractiveAuthentication=no -o PasswordAuthentication=no -o User=ilya -o ConnectTimeout=10 192.168.7.188  
Last login: Thu Feb 13 20:18:45 2014 from 192.168.7.188  
ilya@host1:~$

“previous known host file not found” is an Ansible message. Does anyone know the conditions under which is it triggered?

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [February 13, 2014, 8:44pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/12 "2014-02-13T20:44:18Z")

</div>

You seem to be getting permission denied errors sometimes and other times you can login ok?

Perhaps Ansible is giving the wrong error message but the underlying error is permission denied. Just taking a guess based on the output.

GS

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 13, 2014, 8:46pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/13 "2014-02-13T20:46:01Z")

</div>

No, look closely. I just remove the arguments one by one until it succedes.

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [February 13, 2014, 9:29pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/14 "2014-02-13T21:29:22Z")

</div>

Grepping the code shows it pretty clearly:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def not\_in\_host\_file(self, host):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host\_file = os.path.expanduser(os.path.expandvars("~${USER}/.ssh/known\_hosts"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not os.path.exists(host\_file):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "previous known host file not found"

This is called when a command is executed.

It looks to your user's homedir/.ssh/known\_hosts and if that file doesn't exist, it prints that message you're seeing.

Use of straight ssh will read various config files to determine where the known\_hosts file is, such as /etc/ssh/ssh\_config and ~/.ssh/config

Perhaps in one of those there is a configuration that marks known\_hosts as some other file -- or you have a setting that is preventing ssh from every even checking for known hosts. Either way, that's where the message comes from.

-jlk

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 13, 2014, 9:47pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/15 "2014-02-13T21:47:19Z")

</div>

But the file exists (and the host is there). What could I do to debug it further?

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [February 13, 2014, 9:49pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/16 "2014-02-13T21:49:56Z")

</div>

I would edit the source code and put a debugger statement inside the if statement that does the print. This way when running ansible if you happen to get into this state, you can examine the various data, like what it thinks the value of host\_file is, and then compare that to your filesystem.

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [February 13, 2014, 9:52pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/17 "2014-02-13T21:52:14Z")

</div>

Ok thanks I’ll try that.

---

<div class="post-metadata">

**Author:** ![Steffen\_Prince](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Steffen\_Prince](https://forum.ansible.com/u/Steffen_Prince)\
**Post date:** [August 14, 2014, 11:17pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/18 "2014-08-14T23:17:15Z")

</div>

Sorry for bumping this old thread, but I encountered “previous known host file not found” myself and identified my problem.

I was using ansible on a host where my login name did not match the name I wanted to run ansible under, so I set $USER to the desired name. This is a bad idea generally, but with Ansible will cause the following to fail:

os.path.expanduser(os.path.expandvars(“~${USER}/.ssh/known\_hosts”))

So the correct way to use another name for Ansible ssh is to use the “-u” flag to ansible-playbook. Check to be sure $USER is actually set to your local username if you see this issue.

---

<div class="post-metadata">

**Author:** ![Ilya](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@Ilya](https://forum.ansible.com/u/Ilya)\
**Post date:** [August 15, 2014, 2:01am UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/19 "2014-08-15T02:01:01Z")

</div>

I always set ansible user in the inventory file.

I actually narrowed down the issue. It happens whenever a new host is added in a screen session. So, when working with already known hosts or when screen isn’t used, I don’t see it.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 16, 2014, 2:32pm UTC](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427/20 "2014-08-16T14:32:35Z")

</div>

FYI: Expanding the temp path too early for that setting no longer happens in the latest version of Ansible.

You may wish to try with 1.7.1.

[Next page](https://forum.ansible.com/t/get-rid-of-previous-known-host-file-not-found/14427.md?page=2)
