
Ich bin ein Neuling in Ansible. Ich habe versucht, expect mit Ansible zu verwenden, um die Paketinstallation zu automatisieren (im folgenden Beispiel - Deinstallation). Während der manuellen Deinstallation des Lynx-Pakets erhalte ich folgende Eingabeaufforderung. Im Feld „expect“-Antwort – Ich habe versucht, dieses Muster (beide Zeilen) abzugleichen, aber mein Skript schlägt fehl. Obwohl es für eine Zeile funktioniert – Ist das in Ordnung [j/n]: j. Der reguläre Ausdruck, den ich für beide Zeilen verwendet habe, lautet Installierte Größe: 5,4 M\nIst das in Ordnung [j/n]: j
-----------------------
Installed size: 5.4 M
Is this ok [y/N]:
----------------------
Code 1 (Musterübereinstimmung für beide Zeilen – Fehler):
[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
Ausgabe:
[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]$
========================================================================== Code 2 (Musterübereinstimmung für eine Zeile – funktioniert einwandfrei):
[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
Ausgabe
[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
Antwort1
Besser wäre es, wenn Sie denYum-Modul.
---
- hosts: localhost
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_facts: yes
tasks:
- yum:
name: lynx
state: absent
OderPaket
---
- hosts: localhost
remote_user: ansible
become: yes
become_method: sudo
connection: ssh
gather_facts: yes
tasks:
- pacakge:
name: lynx
state: absent
Sie sollten nach Möglichkeit immer ein Modul anstelle von Befehl/Shell verwenden. Module bieten Ihnen Fehlerprüfung und Idempotenz, ohne dass Sie Ihrer Shell/Ihrem Befehl tonnenweise Logik hinzufügen müssen.
Antwort2
Vielen Dank für eure Vorschläge. Ja, ich werde YUM verwenden. Wie auch immer, der folgende Regex-Ausdruck hat mein Problem gelöst.
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