Ansible の期待正規表現の問題

Ansible の期待正規表現の問題

私は Ansible の初心者です。Ansible で expect を使用してパッケージのインストールを自動化しようとしていました (以下の例では、アンインストール)。lynx パッケージを手動でアンインストールしているときに、次のプロンプトが表示されます。expect 応答フィールドで、このパターン (両方の行) に一致させようとしましたが、スクリプトが失敗しました。1 行では機能しますが、Is this ok [y/N]: y。両方の行で使用した正規表現は、Installed size: 5.4 M\nIs this ok [y/N]: y です。

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

コード 1 (両方の行のパターン一致 - 失敗):

[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

出力:

[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]$ 

================================================================================ コード 2 (1 行のパターン マッチ - 正常に動作):

[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

出力

[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   

答え1

使用する方が良いでしょうyumモジュール

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

またはパッケージ

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

可能な場合は、コマンド/シェルよりも常にモジュールを使用する必要があります。モジュールを使用すると、シェル/コマンドに大量のロジックを追加しなくても、エラー チェックとべき等性が実現します。

答え2

皆さんの提案に感謝します。はい、YUM を使用します。とにかく、以下の正規表現で問題は解決しました。

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

関連情報