Ansible on a linux like OS answer a question I need to answer with yes

1.3k Views Asked by At

I have a DataDomain System which I can enter via ssh. I need to automate a procedure on the DataDomain which does aks a question after a command:

storage add tier active dev3

Object-store is not enabled. Filesystem will use block storage for user data.
    Do you want to continue? (yes|no) [no]: yes

I do try this with Ansible and do use the raw module

- name:  add dev3 active tier
  raw: storage add tier active dev3
  register: RESULT

This is failing with:

TASK [add dev3 active tier] *******************************************************************************************************************************************************************
fatal: [3.127.218.96]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 9, "stderr": "Shared connection to 3.127.218.96 closed.\r\n", "stderr_lines": ["Shared connection to 3.127.218.96 closed."], "stdout": "\r\n**** Could not add storage: system capacity exceeds the limit allowable by the license.\r\n\r\n", "stdout_lines": ["", "**** Could not add storage: system capacity exceeds the limit allowable by the license.", ""]}

the following ansible-playbook is also failing with:

- name:  add dev3 active tier
  raw: | 
    yes | storage add tier active dev3
  register: RESULT

expect module does not accept raw and is also failing.

 - name expect for add dev3 active tier
      expect:
        raw: storage add tier active dev3
        responses:
          Question:
            - Do you want to continue? (yes|no) [no]: y
        timeout: 30 
      register: RESULT

Any idea how I can catch the question and answer with yes?

2

There are 2 best solutions below

0
On

The issue with your expect task is coming from two things:

First, the fact that:

The question, or key, under responses is a python regex match.

Source: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/expect_module.html#notes

So you will have to escape each and every symbol that might have a meaning in a regex.
In your case those symbols are ?, (, |, ), [ and ].
Note: those kind or behaviour are easily testable in regex tools like this one: https://regex101.com/r/ubtVfH/1/

Second, the responses key have two format that you mixed here.

  • Either you have
    responses:
        Question:
          - response1
          - response2
          - response3
    
    Where you don't specify a question from the script but rather a sequential list of answers. This will, of course, only work if the question and responses always comes in the same order.
  • Either you use
    responses:
      This is the prompt of the question in the script: that is the answer
      This is another question: and here is the corresponding answer
    
    And here you are sure you map correctly each answer to the corresponding question. Mind there is no Question key in this dictionary and that the answers are the keys of a dictionary, and not a list.

So for your use case, the proper use of expect would be:

- expect:
    command: storage add tier active dev3
    responses:
      Do you want to continue\? \(yes\|no\) \[no\]: 'yes'
    timeout: 30 
  register: result
0
On

The yes command sends a y by default. But your command is expecting a yes. You can pass the string for yes command to repeat.

- name: add dev3 active tier
  shell:
    cmd: yes yes | storage add tier active dev3
  register: RESULT