In ansible, I can use something like:
debug:var="{{lookup('pipe', 'date +%Y%m%d')}}"
This can work, but what does the 'pipe' mean? cannot find any detail explanation for this in ansible document, want to understand what happens when this statement run.
For example, is the 'date' means run 'date' command from shell? and then use pipe-like way to format the date in the specified way?
Lookup plugins retrieve data from outside sources such as files, databases, key/value stores, APIs, and other services. Like all templating, lookups execute and are evaluated on the Ansible control machine. Ansible makes the data returned by a lookup plugin available using the standard templating system.
Ansible Playbooks are lists of tasks that automatically execute against hosts. Groups of hosts form your Ansible inventory. Each module within an Ansible Playbook performs a specific task. Each module contains metadata that determines when and where a task is executed, as well as which user executes it.
The pipe
is an ansible lookup plugin that will calculate the output of the shell command you specify in lookup's second parameter, and pipe it to the left side of your lookup. You can specify any shell command there.
Therefore in your example, the output of shell command date +%Y%m%d
should be pipe'd to the debug module and var
should be set to this output value.
I generally use pipe
lookup to set a fact for timestamp so that I can append timestamp in a variable at the end for any resource names, like this:
- set_fact: timestamp="{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
Official documentation on Lookups, and specific example that includes pipe
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With