
저는 Ansible의 초보자입니다. 패키지 설치를 자동화하기 위해 Ansible과 함께 Expect를 사용하려고 했습니다(아래 예에서는 제거). Lynx 패키지를 수동으로 제거하는 동안 다음 메시지가 나타납니다. 예상 응답 필드에서 이 패턴(두 줄 모두)을 일치시키려고 했지만 스크립트가 실패했습니다. 한 줄에서는 작동하지만 괜찮나요? [y/N]: y. 두 줄 모두에 사용한 정규 표현식은 Installed size: 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
가능하면 항상 명령/셸 대신 모듈을 사용해야 합니다. 모듈은 쉘/명령에 수많은 논리를 추가하지 않고도 오류 검사 및 멱등성을 제공합니다.
답변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