# Using local\_action and set\_fact to gather local machine facts

**URL:** <https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065>\
**Category:** Ansible Project\
**Created:** [July 24, 2017, 10:09am UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065 "2017-07-24T10:09:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [July 24, 2017, 10:09am UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/1 "2017-07-24T10:09:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 24, 2017, 11:31am UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/2 "2017-07-24T11:31:11Z")

</div>

The correct syntax is

&nbsp;&nbsp;&nbsp;- name: Gathering and setting Facts for this deploy  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module: set\_fact  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;- name: Gathering and setting Facts for this deploy  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 24, 2017, 2:22pm UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/3 "2017-07-24T14:22:14Z")

</div>

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:

&nbsp;&nbsp;- name: Gathering and setting Facts for this deploy  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date\_time: '{{ hostvars['localhost']['ansible\_date\_time]['date']  
+ '\_' + hostvars['localhost']['ansible\_date\_time]['time'] }}'

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [July 24, 2017, 8:40pm UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/4 "2017-07-24T20:40:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 24, 2017, 8:48pm UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/5 "2017-07-24T20:48:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 24, 2017, 9:14pm UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/6 "2017-07-24T21:14:24Z")

</div>

That's easy to fix.

"{{ lookup('pipe', 'date +%F\_%T') }}"

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

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [July 25, 2017, 12:56pm UTC](https://forum.ansible.com/t/using-local-action-and-set-fact-to-gather-local-machine-facts/25065/7 "2017-07-25T12:56:42Z")

</div>

> 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
