Using local_action and set_fact to gather local machine facts

Hello

I am trying to gather facts (date and time) on the local machine (Ansible Host) using (local_action) before the rest of the playbook continues:

  • name: Gathering and setting Facts for this deploy

local_action:

set_fact:

date_time: ‘{{ ansible_date_time.date }}_{{ ansible_date_time.time }}’

Basically, the remote host is in a different time zone, but I would like to use the date of the Ansible Host for creating directories etc.

But I keep seeing the following error:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in ‘/usr/playbooks/roles/websites-backup/tasks/main.yml’: line 3, column 3, but may

be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • name: Gathering and setting Facts for this deploy

^ here

The error appears to have been in ‘/usr/playbooks/roles/websites-backup/tasks/main.yml’: line 3, column 3, but may

be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • name: Gatherin and setting Facts for this deploy

^ here

Regards
JS

The correct syntax is

   - name: Gathering and setting Facts for this deploy
     local_action:
       module: set_fact
       date_time: '{{ ansible_date_time.date }}_{{ ansible_date_time.time }}'

But this will not do what you are trying to do.

Since lookup is executed on the localhost you can use this task instead.

   - name: Gathering and setting Facts for this deploy
     set_fact:
       date_time: '{{ lookup('pipe', 'date') }}'

With this task every host will have the variable date_time set to the time of the Ansible control machine.

To clear up, using local_action does not affect 'controller only'
actions like set_fact/group_by/add_hosts/etc.

Also using local_action will ONLY use the localhost 'connection vars',
the rest of the vars will belong to the inventory_hostname (this is
true for all delegation).

Aside from the solution above, another option, if you gathered facts
from localhost:

  - name: Gathering and setting Facts for this deploy
    set_fact:
      date_time: '{{ hostvars['localhost']['ansible_date_time]['date']
+ '_' + hostvars['localhost']['ansible_date_time]['time'] }}'

Hi Kai, Brian

Some fantastic suggestions there!

@Kai your suggestion is greater however, the output is in the format:
Mon 24 Jul 21:23:08 BST 2017

I was looking for it to be in the format: 2017-07-24_02:26:08 - as I will be using that to create the directory names.

@Brian I think your suggestions looks like it’s going to do exactly what I want, however when I ran it, I kept getting the following errors:

“failed”: true,
“msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘dict object’ has no attribute ‘ansible_date_time’\n\nThe error appears to have been in ‘/usr/playbooks/website-playbooks/roles/websites-backup/tasks/main.yml’: line 14, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Gathering and setting Facts for this deploy\n ^ here\n”
}
to retry, use: --limit @/usr/playbooks/website-playbooks/websites-backup.retry

I tried a few modifications of it, but I didn’t have much luck…

Regards
Jinal

You seem to be missing facts for localhost, you need to gather them first.

That's easy to fix.

"{{ lookup('pipe', 'date +%F_%T') }}"

Check out "man date" to learn more about date's capabilities.

Hi Kai

Ah perfect thanks! I should have know that one! Sorry, didn’t realise that it picked that up from the native OS!

Cheers
JS