Ansible issues with locate command

August 5, 2019 269 views
Ansible

What I am trying to accomplish:

  1. Get list of commands that I want to run:
    [db2inst1@db2server1 ~]$ locate db2ilist | grep instance
    /opt/ibm/db2/V10.5/instance/db2ilist
    /opt/ibm/db2/V11.1/instance/db2ilist

  2. Execute both of the commands above

First issue is that Ansible does not seem to be playing well with the locate command:

- name: Update locate database
  command: /bin/updatedb

- name: Gather DB2 DB2 instances
  command: /bin/locate 'db2ilist' | /bin/grep 'instance'
  register: db2ilist_output

I get a rather large list of all files that have ‘instance’ in the name somewhere. I expected to only get the 2 files listed above.

Second issue is… once I have both executables inside the db2ilist_output register, how do I loop through them to execute them?

1 Answer

Was able to resolve the questions with:

- name: Update locate database
  command: /bin/updatedb

- name: Gather DB2 DB2 instances
  shell: /bin/locate 'db2ilist' | /bin/grep 'instance'
  register: db2ilist_cmd

- debug: msg="{{ db2ilist_cmd.stdout_lines }}"

- name: List each DB2 instance
  command: "{{ item }}"
  with_items: "{{ db2ilist_cmd.stdout_lines}}"
  register: db2ilist_output
Have another answer? Share your knowledge.