Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if list of strings are present in command output in Ansible?

Tags:

I want to run an Ansible action on the condition that a shell command doesn't return the expected output. ogr2ogr --formats pretty-prints a list of compatible file formats. I want to grep the formats output, and if my expected file formats aren't in the output, I want to run a command to install these components. Does anyone know how to do this?

- name: check if proper ogr formats set up   command: ogr2ogr --formats | grep $item   with_items:     - PostgreSQL     - FileGDB     - Spatialite   register: ogr_check  # If grep from ogr_check didn't find a certain format from with_items, run this - name: install proper ogr formats   action: DO STUFF   when: Not sure what to do here 
like image 750
Tanner Semerad Avatar asked Nov 12 '13 23:11

Tanner Semerad


People also ask

How do you show output in Ansible?

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.

How do you grep Ansible output?

There is no Ansible grep module, but you can use the grep commands along with shell module or command module. We can store the results of the task and use it in various conditional statements, print them or use them for debugging purposes.

What is RC in Ansible output?

rc. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on), this field contains 'return code' of these utilities.

What is register in Ansible?

Ansible register is a way to capture the output from task execution and store it in a variable. This is an important feature, as this output is different for each remote host, and the basis on that we can use conditions loops to do some other tasks. Also, each register value is valid throughout the playbook execution.


1 Answers

First, please make sure you are using Ansible 1.3 or later. Ansible is still changing pretty quickly from what I can see, and a lot of awesome features and bug fixes are crucial.

As for checking, you can try something like this, taking advantage of grep's exit code:

- name: check if proper ogr formats set up   shell: ogr2ogr --formats | grep $item   with_items:     - PostgreSQL     - FileGDB     - Spatialite   register: ogr_check   # grep will exit with 1 when no results found.    # This causes the task not to halt play.   ignore_errors: true  - name: install proper ogr formats   action: DO STUFF   when: ogr_check|failed 

There are some other useful register variables, namely item.stdout_lines. If you'd like to see what's registered to the variable in detail, try the following task:

- debug: msg={{ogr_check}} 

and then run the task in double verbose mode via ansible-playbook my-playbook.yml -vv. It will spit out a lot of useful dictionary values.

like image 112
Dan Avatar answered Sep 29 '22 02:09

Dan