Using local_action and set_fact to gather local machine facts

2,340 views
Skip to first unread message

JS

unread,
Jul 24, 2017, 6:09:44 AM7/24/17
to Ansible Project
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

Kai Stian Olstad

unread,
Jul 24, 2017, 7:31:31 AM7/24/17
to ansible...@googlegroups.com
On 24.07.2017 12:09, JS wrote:
> 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 }}'
>

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.


--
Kai Stian Olstad

Brian Coca

unread,
Jul 24, 2017, 10:22:29 AM7/24/17
to Ansible Project
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'] }}'



--
----------
Brian Coca

JS

unread,
Jul 24, 2017, 4:40:42 PM7/24/17
to Ansible Project
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

Brian Coca

unread,
Jul 24, 2017, 4:48:19 PM7/24/17
to Ansible Project
You seem to be missing facts for localhost, you need to gather them first.

---------------
Brian Coca

Kai Stian Olstad

unread,
Jul 24, 2017, 5:14:49 PM7/24/17
to ansible...@googlegroups.com
On 24. juli 2017 22:40, JS wrote:
> 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.

That's easy to fix.

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

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


--
Kai Stian Olstad

JS

unread,
Jul 25, 2017, 8:56:42 AM7/25/17
to Ansible Project, ansible-pr...@olstad.com
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 
Reply all
Reply to author
Forward
0 new messages