I wanted to set a value inside a ini_file, but this value is an MD5 hash of the current time. (I am not afraid of accidentally replacing the value, or magically running it twice and having the same value in two different servers.)
This is what I tried, but got only the command as the value in the file (not sure why I thought it would work…):
- name: Replace HardwareID with new MD5
ini_file:
path: /etc/app/config.ini
section: DEFAULT
option: hardware_token
value: $(date | md5sum | cut -d" " -f1)
Is there a simple way to make it work?
Best Answer
Q: "How to use the output of command inside Ansible ini_file value?"
A: Register the result of the command and use it as the value, e.g.
gives (running with --check --diff)
It's easier to use Ansible facts if you want to use data and time as the input. For example, the dictionary ansible_date_time keeps the date and time if you gather the facts. In the playbook, we set
gather_facts: false
. Therefore the dictionary is not definedgives
You'll have to either gather the facts
gather_facts: true
when you start the play or runsetup
, e.g.gives
This is not very practical because to get the current time you have to run
setup
. Instead, the filter strftime gives you always the current time, e.g.gives
As a side note: Using this hash might be very quick searching if the date-time (in granularity of seconds) is the index.