ITJunkie
ITJunkie

Reputation: 3

How to get facts at each command in Ansible

Ansible get facts only at start. But i need check facts at each commands. For example:

I need create a directory, after that i need put file to this directory. But ansible get fact 'dir doesn't exist' at start, create dir and at next step fact still FALSE and ansible skip this step =( And do this step only after second run.

I'll try setup after all steps to gathering facts again but it doesn't work.

I do it like this:

 - stat: path=/etc/zabbix/scripts/rabbitmq
   register: rmqscriptdir

 - name: Create scripts dir if not exist
   when: rmqscriptdir.stat.exists == False
   shell: mkdir /etc/zabbix/scripts/rabbitmq

 - name: Gathering facts again
   setup:

 - name: Set owner and permissions to rabbitmq directory
   when: rmqscriptdir.stat.exists == True
   file: path=/etc/zabbix/scripts/rabbitmq owner=zabbix group=root mode=0750


 - stat: path=/etc/zabbix/scripts/rabbitmq/api.py
   register: rmqscript_api

 - name: Create api.py if not exist 
   when: rmqscript_api.stat.exists == False and rmqscriptdir.stat.exists == True
   shell: cd /etc/zabbix/scripts/rabbitmq; wget https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py

 - name: Gathering facts again
   setup:

 - name: Set owner and permissions to api.py
   when: rmqscript_api.stat.exists == True
   file: path=/etc/zabbix/scripts/rabbitmq/api.py owner=zabbix group=root mode=0755

Upvotes: 0

Views: 1712

Answers (1)

udondan
udondan

Reputation: 59989

I think you misunderstand what the setup module does. By registering a value it does not become a fact that will be reloaded by the setup module when run again. Your registered value stays the same. If you want to check again if a path exists you do not need to re-run the setup module, but the stats module and again register its output.

But anyway, the idea of Ansible is actually to not manually check if every task should be executed or not. That is something Ansible takes care for you, Ansible in general is indepotent, meaning it will have the same result no matter how many times you run the play.

Here is a cleaned up version, which creates a folder and downloads the file. If the folder already exists, the 1st task will do nothing. If the file api.py already exists, the 2nd task will do nothing.

- name: Create scripts dir if not exist
  file:
    path: /etc/zabbix/scripts/rabbitmq
    state: directory
    owner: zabbix
    group: root
    mode: 0750

- name: Create api.py if not exist
  get_url:
    url: https://raw.githubusercontent.com/jasonmcintosh/rabbitmq-zabbix/master/scripts/rabbitmq/api.py
    dest: /etc/zabbix/scripts/rabbitmq/api.py
    owner: zabbix
    group: root
    mode: 0755

PS: If you want to see which values are reloaded by the setup module, you can register its output and show it in a debug task, like so:

- setup:
  register: all_server_facts

- debug:
    var: all_server_facts

This only contains server facts, info about cpu, hard drives, network etc. Also see this answer for an example output.

Upvotes: 1

Related Questions