# I am trying to run ansible from cron and looking for some advice

**URL:** <https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290>\
**Category:** Ansible Project\
**Created:** [August 13, 2013, 5:12pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290 "2013-08-13T17:12:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jimmy\_Ezell](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jimmy\_Ezell](https://forum.ansible.com/u/Jimmy_Ezell)\
**Post date:** [August 13, 2013, 5:12pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/1 "2013-08-13T17:12:35Z")

</div>

The goal is to create a Amazon instance from a script.  
This works fine if I am logged in as the user account but not from cron.  
I believe SSH authentication is the problem. I am specifing the key in the ansible-playbook command but still not working.  
Cron calls a perl script:

- 
  - 
    - 
      - 
        - ec2-user /iusr/bin/perl /home/ec2-user/test.pl \>\> /home/ec2-user/out

Perl Script Sets Variables Like this:

my $AnsiblePath=‘/home/ec2-user/ansible’;  
my $AnsiblePlaybook=‘/home/ec2-user/create.yml’;  
my $AzServerKey=‘/home/ec2-user/.ssh/taz-use-key.pem’;  
my $AnsibleInventory=‘/home/ec2-user/ansible\_hosts’;

$ENV{AWS\_ACCESS\_KEY\_ID}=“MyAccessKey”;  
$ENV{AWS\_SECRET\_ACCESS\_KEY}=“SecretKey”;  
$ENV{ANSIBLE}=“$AnsiblePath”;  
$ENV{PATH}=“$ENV{PATH}:$ENV{ANSIBLE}/bin”;  
$ENV{PYTHONPATH}=“/home/ec2-user/ansible/lib:”;  
$ENV{ANSIBLE\_LIBRARY}=“$ENV{ANSIBLE}/library”;  
$ENV{MANPATH}=“$ENV{ANSIBLE}/docs/man:”;

`ansible-playbook $AnsiblePlaybook -i $AnsibleInventory --private-key=$AzServerKey`;

The Error I get is:

output is:

PLAY [provision instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1] =\> {‘msg’: ‘FAILED: argument must be an int, or have a fileno() method.’, ‘failed’: True}

TASK: [Launch new EC2 instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
FATAL: no hosts matched or all hosts have already failed – aborting

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
to retry, use: --limit @/var/tmp/ansible/create.retry

---

<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:** [August 14, 2013, 12:29am UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/2 "2013-08-14T00:29:47Z")

</div>

This is because you have host key checking turned on and it’s asking for user input when it sees new hosts.

I agree the error message should be better! (I’ll file a ticket on this).

I’d suggest turning off host key checking for new hosts in the ansible.cfg file.

–Michael

---

<div class="post-metadata">

**Author:** ![Jimmy\_Ezell](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jimmy\_Ezell](https://forum.ansible.com/u/Jimmy_Ezell)\
**Post date:** [August 15, 2013, 5:21pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/3 "2013-08-15T17:21:10Z")

</div>

Thanks I will give that a try.

---

<div class="post-metadata">

**Author:** ![Jimmy\_Ezell](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jimmy\_Ezell](https://forum.ansible.com/u/Jimmy_Ezell)\
**Post date:** [September 6, 2013, 6:42pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/4 "2013-09-06T18:42:24Z")

</div>

Adding the line below in ansible.cfg did not help. any other ideas?  
host\_key\_checking = False

---

<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:** [September 7, 2013, 12:42pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/5 "2013-09-07T12:42:29Z")

</div>

That is quite a delay in retesting 🙂

We should have already fixed this upstream in 1.3, if you’d like to try there.

---

<div class="post-metadata">

**Author:** ![Jimmy\_Ezell](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jimmy\_Ezell](https://forum.ansible.com/u/Jimmy_Ezell)\
**Post date:** [September 11, 2013, 3:22pm UTC](https://forum.ansible.com/t/i-am-trying-to-run-ansible-from-cron-and-looking-for-some-advice/13290/6 "2013-09-11T15:22:06Z")

</div>

If I ping the new server I created in ec2 it fails the first time and second time. By the the third time it seems to work. Does not seem to be a timing issue as waiting longer to do the first ping seems to make no difference. As a work around I ping several times in a loop and look for a success.
