Ansible 期望正規表示式問題

Ansible 期望正規表示式問題

我是 Ansible 的新手。我試圖將 Expect 與 Ansible 一起使用來自動安裝軟體包(在下面的範例中 - 卸載)。在手動卸載 lynx 軟體包期間,我收到以下提示。在期望響應字段中 - 我試圖匹配此模式(兩行),但我的腳本失敗。雖然它適用於一行 - 這可以嗎 [y/N]: y.我用於兩條線的正規表示式安裝大小:5.4 M\n這樣可以嗎[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(一行的模式匹配 - 工作正常):

[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

你最好使用百勝模組

---
- 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

如果可能的話,您應該始終在命令/shell 上使用模組。模組為您提供錯誤檢查和冪等性,而無需向 shell/命令添加大量邏輯。

答案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

相關內容