Skip to content

Instantly share code, notes, and snippets.

@djoreilly
Created March 10, 2015 13:52
Show Gist options
  • Save djoreilly/86e233f225f2c63d633a to your computer and use it in GitHub Desktop.
Save djoreilly/86e233f225f2c63d633a to your computer and use it in GitHub Desktop.
howto augment ansible facts
# http://jpmens.net/2012/07/15/ansible-it-s-a-fact/
#$ cat count_users
##!/bin/sh
#COUNT=`who | wc -l`
#cat <<EOF
#{
# "ansible_facts" : {
# "users_logged_in" : $COUNT
# }
#}
#EOF
# export ANSIBLE_LIBRARY=<path-to count_users>
---
- name: Test augment Ansible facts
hosts:
- node1
user: vagrant
sudo: False
gather_facts: True
tasks:
- action: count_users
- debug: msg="the hostname is {{ inventory_hostname }}"
- debug: msg="there are {{ users_logged_in }} users logged in"
@djoreilly
Copy link
Author

PLAY [Test augment Ansible facts] *********************************************

GATHERING FACTS ***************************************************************
ok: [node1]

TASK: [count_users] ***********************************************************
ok: [node1] => {"ansible_facts": {"users_logged_in": 3}}

TASK: [debug msg="the hostname is {{ inventory_hostname }}"] ******************
ok: [node1] => {
"msg": "the hostname is node1"
}

TASK: [debug msg="there are {{ users_logged_in }} users logged in"] ***********
ok: [node1] => {
"msg": "there are 3 users logged in"
}

PLAY RECAP ********************************************************************
node1 : ok=4 changed=0 unreachable=0 failed=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment