Ansible espera problema de regex

Ansible espera problema de regex

Eu sou um novato em Ansible. Eu estava tentando usar o expect com o Ansible para automatizar a instalação do pacote (no exemplo abaixo - desinstalar). Durante a desinstalação manual do pacote lynx, recebo o seguinte prompt. No campo de resposta esperada - eu estava tentando corresponder a esse padrão (ambas as linhas), mas meu script falhou. Embora funcione para uma linha - Tudo bem [s/N]: y. Regex que usei para ambas as linhas é Tamanho instalado: 5,4 M\nTudo bem [s/N]: y

-----------------------
Installed size: 5.4 M
Is this ok [y/N]: 
----------------------

Código 1 (correspondência de padrão para ambas as linhas - falha):

[ansible@localhost ansible]$ cat main.yml
---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - name: remove a package 
    expect:
      command: yum remove lynx
      responses:
        Installed size: 5.4 M\nIs this ok \[y\/N\]: y 
        #Is this ok \[y\/N\]: y
      echo: yes

Saída:

[ansible@localhost ansible]$ ansible-playbook main.yml
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in version 2.8. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/etc/ansible/main.yml': line 13, column 51, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      responses:
        Installed size: 5.4 M\nIs this ok \[y\/N\]: y
                                                  ^ here

exception type: <class 'yaml.scanner.ScannerError'>
exception: mapping values are not allowed in this context
  in "<unicode string>", line 13, column 51
[ansible@localhost ansible]$ 

================================================= ===================== Código 2 (correspondência de padrão para uma linha - funciona bem):

[ansible@localhost ansible]$ cat main.yml
---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - name: remove a package 
    expect:
      command: yum remove lynx
      responses:
        #Installed size: 5.4 M\nIs this ok \[y\/N\]: y 
         Is this ok \[y\/N\]: y
      echo: yes

Saída

[ansible@localhost ansible]$ 

[ansible@localhost ansible]$ ansible-playbook main.yml
[DEPRECATION WARNING]: DEFAULT_SUDO_USER option, In favor of become which is a generic framework . This feature will be removed in version 2.8. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [localhost] *********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [127.0.0.1]

TASK [remove a package] **************************************************************************************************************************************
changed: [127.0.0.1]

PLAY RECAP ***************************************************************************************************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0   

Responder1

Seria melhor você usar omódulo yum.

---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - yum: 
      name: lynx
      state: absent

Oupacote

---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - pacakge: 
      name: lynx
      state: absent

Você deve sempre usar um módulo sobre comando/shell quando possível. Os módulos fornecem verificação de erros e idempotência sem a necessidade de adicionar toneladas de lógica ao seu shell/comando.

Responder2

Obrigado pessoal por suas sugestões. Sim, vou usar YUM. De qualquer forma, a expressão regex abaixo resolveu meu problema.

Installed.*\nIs this ok \[y\/N\]: y


Code:
---
- hosts: localhost 
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
  - name: remove a package 
    expect:
      command: yum remove lynx
      responses:
        Installed.*\nIs this ok \[y\/N\]: y 
        # Is this ok \[y\/N\]: y
      echo: yes

informação relacionada