Ansible 期望正则表达式问题

Ansible 期望正则表达式问题

我是 Ansible 的新手。我尝试使用 Ansible 的 expect 来自动化软件包安装(以下示例中为卸载)。在手动卸载 lynx 软件包期间,我收到以下提示。在 expect 响应字段中 - 我尝试匹配此模式(两行),但我的脚本失败了。虽然它适用于一行 - 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(一行的模式匹配 - 工作正常):

[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

尽可能使用模块而不是命令/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

相关内容