2

I am writing the role which includes some tasks to be executed on local machine using dedicate_to: localhost statements.

When gathering facts, ansible registers some variables about the remote host (e.g. ansible_os_family or ansible_system etc.)

How could I detect local (not a remote) OS/OS family? Can it be achieved with facts gathering? Or is there any other proper way of doing it?

1 Answer 1

1

Ansible can gather facts about any host that you tell it, whether it be a remote server or the local host from where you're executing ansible/ansible-playbook.

Example

Sample inventory:

$ cat local_inv
[local]
localhost   ansible_connection=local

Sample playbook:

$ cat os.yml
- name: OS family
  hosts: all

  tasks:
    - name: print OS Family
      debug: var=ansible_os_family

Example Run:

$ ansible-playbook -i local_inv os.yml

PLAY [OS family] *********************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [print OS Family] ***************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "ansible_os_family": "Darwin"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .