Need help in using set_fact , playbook runs fine , facts not set , no errors

257 views
Skip to first unread message

Marc Roelofs

unread,
Nov 19, 2014, 5:25:36 AM11/19/14
to ansible...@googlegroups.com
Hi Group, 

I am trying to add some basic facts to a system but nothing seems to work. 
My goal is to add a simple fact like the type of the server and the main application running on it  to be set 
Type of server can be webserver (apache/tomcat)/ database server (mysql/postgress .. etc)  
And I would like to do this from the commandline  with -e at initial deployment of a server. 

My main.yml looks like this 
__________________________________________________________________________
---
# Task to set facts on system, based on commandline variables
- name: Set system facts
  action: set_fact system_type={{system_type}}
  action: set_fact main_app={{main_app}}
_________________________________________________________________________
and the result = 

TASK: [system-config | Set system facts] **************************************
ok: [my-targeted-machine]


I tried several forms of code like suggested in the http://docs.ansible.com/set_fact_module.html page  
____________________________________________________________________________
---
# Task to set facts on system, based on commandline variables
- name: Set system facts
  set_fact: system_type="{{system_type}}" main_app="{{main_app}}"
____________________________________________________________________________

Has the same result 
ok: [my-targeted-machine]

Or 
____________________________________________________________________________
---
# Task to set facts on system, based on commandline variables
- name: Set system facts
- set_fact: system_type="{{system_type}}" main_app="{{main_app}}"
____________________________________________________________________________

Has this result : ERROR: 'action' or 'local_action' attribute missing in task "Set system facts"



And when removing the -name: Set system facts line I can see my variables are used from the commandline :-) 
$ ansible-playbook -i 'mytargeted-machine,' all.yml --extra-vars '{"system_type":"webservert","main_app":"apache2"}'

TASK: [system-config | set_fact system_type="webservert" main_app="apache2"] ***
ok: [my-targeted-machine]


So what would be the best approach to set a fact ??
Ultimate goal: use fact caching and targeting machines with specific facts !



Brian Coca

unread,
Nov 19, 2014, 10:31:02 AM11/19/14
to ansible...@googlegroups.com
First, set_fact is ephemeral (only lasts for the current ansible run), your first 2 attempts were succeeding, to see the results you can run:
 - debug: msg="systemtype:{{system_type}}" 

second, the - denotes a task, so - name + - action doen't work, name is a property of the task and has to be in the same grouping.

- name: 
  action:

or 

- action:
  name: 

you can add this information permanently either by adding it as group_var/host_var or you can use a file in /etc/ansible/facts.d on the target machines

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages