Share value between hosts in playbook

62 views
Skip to first unread message

Gaetano Mazzeo

unread,
Sep 9, 2015, 1:50:47 PM9/9/15
to Ansible Project
Hi,
I need to do the following thing.
On the first host, calculate the checksum of a certain file (es. /etc/hosts) and save it into a variable, let's say "file_checksum".
On the second host, I would like to calculate newly the checksum and compare it with the one from the first host (i.e. the value saved in "file_checksum"). And so on with the other hosts of my inventory.
I tried several ways without success. Also run_once on set_fact doesn't seem to work (see below the snippet not fully working).
Any Idea how to save a value between hosts in a playbook?
Thanks in advance.
Cheers.
Gaetano

---
- name: check_configuration
  hosts: all
  serial: 1
  gather_facts: yes
   
  tasks:
 
  - name: Execution of the stat on the file
    stat: path=/etc/hosts
    register: stat_results

  - name: Check of file existence
    fail:
      msg: "The file doesn't exist!"
    when: stat_results.stat.exists|bool == false

  - debug: var=file_checksum
    when: file_checksum is defined

  - name: Set fact   
    set_fact:
      file_checksum: "{{ stat_results.stat.checksum }}"
    run_once: true

  - name: Compare of the file file_checksum
    debug: var="Checksum of the file is {{ file_checksum }} but right is {{ stat_results.stat.checksum }}"
    when: file_checksum != "" and (file_checksum != stat_results.stat.checksum)

  - name: Compare of the file_checksum with the first value
    fail:
      msg:  var="Checksum of the file is {{ file_checksum }} but right is {{ stat_results.stat.checksum }}"
    when: file_checksum != "" and file_checksum != stat_results.stat.checksum

James Cammarata

unread,
Sep 11, 2015, 2:52:19 AM9/11/15
to ansible...@googlegroups.com
You can share values between hosts using the hostvars[host_name] variable, however I believe in your situation you'll want to run this in separate plays, as these task will still run on all of your systems (just one system at a time, due to the serial use).

James Cammarata
Director, Ansible Core Engineering
github: jimi-c

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6473142a-d868-45a3-b929-3f0137415fa7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages