Ansible - regex help

27 views
Skip to first unread message

Michael

unread,
Nov 19, 2019, 1:50:04 PM11/19/19
to Ansible Project
I'm creating a script that dynamically generates a response file for an uninstall and stores the response file in a var for reuse in the uninstall command. My play :

=============================================
- name:  UnInstall Oracle
  hosts: localhost
  tags: oracle
  become: yes
  become_method: sudo

  tasks:
  - name: Generate Response File for Uninstall
    command: /bin/sh -c  "/opt/oracle/product/12.1.0/deinstall/deinstall -silent -checkonly"
    register: result
    become: true
    become_user: orclient
    become_method: sudo

  - name: Set Response Variable - respfile
    set_fact:
      respfile: "{{ result.stdout | regex_search(regexp,'\\1') }}"
    vars:
      regexp: 'Location of response file generated...([^"]+rsp)'

#  - name: Debug
#    debug:
#      var: respfile
     
  - name: Uninstall old oracle client
    command: /bin/sh -c "/opt/oracle/product/12.1.0/deinstall/deinstall -silent -paramfile {{ respfile }}"
    become: true
    become_user: orclient
    become_method: sudo

=============================================

My debug task when run prints as follows:

TASK [Debug] ***************************************************************************************************************
ok: [localhost] => {
    "respfile": [
        "/tmp/deinstall2019-11-19_11-29-40AM/response/deinstall_OraClient12Home1.rsp"
    ]
}


And my uninstall task fails with error message:


fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/opt/oracle/product/12.1.0/deinstall/deinstall -silent -paramfile [u'/tmp/deinstall2019-11-19_11-35-44AM/response/deinstall_OraClient12Home1.rsp']",

The error shows added characters to the response file path and that causes it to fail.

How can i make the response file appear without the added characters?

Vladimir Botka

unread,
Nov 19, 2019, 3:25:36 PM11/19/19
to Michael, ansible...@googlegroups.com
On Tue, 19 Nov 2019 10:50:04 -0800 (PST)
Michael <michae...@gmail.com> wrote:

> - name: Uninstall old oracle client
> command: /bin/sh -c "/opt/oracle/product/12.1.0/deinstall/deinstall
> -silent -paramfile {{ respfile }}"
> [...]
> "respfile": [
> "/tmp/deinstall2019-11-19_11-29-40AM/response/deinstall_OraClient12Home1.rsp"
> ]
> [...]
> fatal: [localhost]: FAILED! => {"changed": true, "cmd":
> "/opt/oracle/product/12.1.0/deinstall/deinstall -silent -paramfile
> [u'/tmp/deinstall2019-11-19_11-35-44AM/response/deinstall_OraClient12Home1.rsp']",

(<path> and <params> replaced for readability)
*respfile* is a list. It's possible to run the command with the first item

- name: Uninstall old oracle client
command: /bin/sh -c "<path>/deinstall <params> {{ respfile.0 }}"

,or in the loop if there might be more items to process

- name: Uninstall old oracle client
command: /bin/sh -c "<path>/deinstall <params> {{ item }}"
loop: "{{ respfile }}"

Cheers,

-vlado

J Hawkesworth

unread,
Nov 19, 2019, 3:35:17 PM11/19/19
to Ansible Project
I think the issue is your respfile fact contains a list, not a string, hence the [ ] in the Debug output.

There are a couple of ways to fix that.  You can either do it when you are creating the fact (use the 'first' filter to just get the first element of the list), like this.

  - name: Set Response Variable - respfile
    set_fact:
      respfile: "{{ result.stdout | regex_search(regexp,'\\1') |first}}"

Or you could do it when you use the respfile fact

  - name: Uninstall old oracle client
    command: /bin/sh -c "/opt/oracle/product/12.1.0/
deinstall/deinstall -silent -paramfile {{ respfile|first }}"
    become: true
    become_user: orclient
    become_method: sudo

Instead of using a filter you can use array syntax:

  - name: Uninstall old oracle client
    command: /bin/sh -c "/opt/oracle/product/12.1.0/
deinstall/deinstall -silent -paramfile {{ respfile[0] }}"
    become: true
    become_user: orclient
    become_method: sudo

Hope this helps,

Jon

Michael

unread,
Nov 20, 2019, 12:15:11 AM11/20/19
to Ansible Project
Amazing. These solutions worked! Thank you!!
Reply all
Reply to author
Forward
0 new messages