I have written an ansible script that returns some information from various sources. One of the variables I am saving in a variable during a task is the number of records in a certain mysql database table.
I can print out results in the playbook quite well. What I want to do however is write the results from all hosts in a single (csv) file on the master/control server or computer running the playbook
- name: Show results
debug:
msg: "URL: {{hostvars[inventory_hostname]['ansible_nodename']}} RECORDCOUNT: {{results.stdout}} BASE VERSION: {{baseversion.stdout}}"
this piece prints the info I want for 20 nodes. I now want to write this line in a single file on the master server. ( csv )
I've tried various things with local copy but until now to no avail
To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.
This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name csvfile even without specifying the collections: keyword.
Actually no more data is needed. This ansible playbook is very basic, and can be used for small enterprise switches much like f.e. the Catalyst 1000, Catalyst 2000 and Catalyst 3000 series. This will not work with Catalyst 4500 and bigger modular switches, only because of the hardcoded flash name in the script.
Red Hat subscribers, select 2.9 in the version selection to the left for the most recent Red Hat release. This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name csvfile even without specifying the collections: keyword.
name of the CSV/TSV file to open. The default is for TSV files (tab delimited) not CSV (comma delimited) … yes the name is misleading. As of version 2.11, the search parameter (text that must match the first column of the file) and filename parameter can be multi-word.
shell
module can come up handy when no other solution visible:
- name: Save results
shell: echo URL: {{hostvars[inventory_hostname]['ansible_nodename']}} RECORDCOUNT: {{results.stdout}} BASE VERSION: {{baseversion.stdout}} >> /opt/my_file.log
delegate_to: localhost
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