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

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

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

Thanks I will give that a try.

Adding the line below in ansible.cfg did not help. any other ideas?
host_key_checking = False

That is quite a delay in retesting :slight_smile:

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

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.