以下は、シスコ デバイスの Ansible 構成用の yaml ファイルです。/etc/ansible/hosts では、以下に示すように、Amazon EC2 Ami インスタンスを反映するように hosts ファイルも編集しました。
[ec2-instances]
ec2-54-152-72-23.compute-1.amazonaws.com
インベントリファイル^
YAMLファイル以下
---
- hosts: ec2-54-152-72-23.compute-1.amazonaws.com
gather_facts: false
connection: local
tasks:
-name: Customer IOS Upgrade Initial Discovery
cli_command: "{{ show }}"
vars:
show:
- show run
- show version
- show interface status
- show license
-mail:
host: smtp.gmail.com
port: 587
username: [email protected]
password: sample2
to: [email protected]
subject: '{{ ansible_hostname }} configuration'
body: 'System {{ ansible_hostname }} has been successfully discovered.'
delegate_to: localhost
save_when: changed
...
以下を実行しようとすると、次のエラー メッセージが表示されます。なぜこのようなことが起こるのか、理由がわかりますか? [ec2-instances] で提供される初期の .yaml ファイルに表示されているものを切り替えましたが、実行結果に違いはありません。
ansible-playbook -i /etc/ansible/hosts test2.yml --user ec2-user --private-key=/home/adam/Desktop/EC2CSR1000v.pem -vvv
ansible-playbook 2.7.9
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/adam/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Using /etc/ansible/ansible.cfg as config file
/etc/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/etc/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected
Parsed /etc/ansible/hosts inventory source with ini plugin
ERROR! A malformed block was encountered while loading tasks
The error appears to have been in '/etc/ansible/scripts/test2.yml': line 2, column 4, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- hosts: [ec2-instance]
^ here
答え1
まず、純粋なヤム。
タスクにグローバルに関連するオプション、または特定のモジュールのパラメータであるオプションには注意する必要があります。正しいスコープを反映するために、それらのオプションは正しくインデントする必要があります。
使用したいモジュールに関するドキュメントを必ず読む必要があります。たとえば、cli_command
文字列を直接受け入れますが、明示的なcommand
オプションとその他のオプションも必要になります。リストは受け入れられないため、他のコマンドをループする必要があります。
最後に、確認すべき奇妙な点がいくつかあります。
- プレイのターゲットとして単一のリモート ホストを指定していますが (問題ありません)、接続はローカルとしてリストされています。これが実際にリモート ホストである場合は、SSH で接続する必要があるため、これは機能しません。
- あなたは(私がそう思う)タスクオプションを使用しています
save_when
。私はそれが誤ってファイルの最後にあり、cli_command
モジュール呼び出しで使用する必要があることを理解していると思います。一方、それはこのモジュールのパラメータとしてリストされていません(しかし、他の関連するシスコ iOS モジュール
現時点では、あなたが何をしたいのか正確にはわかりません。そのため、私が正しいと思う方法でプレイブックを修正し、あなたが前進できるようにすることしかできません。
---
- name: Playbook to manage my cisco IOS devices
hosts: ec2-54-152-72-23.compute-1.amazonaws.com
gather_facts: false
tasks:
- name: Customer IOS Upgrade Initial Discovery
cli_command:
command: "{{ item }}"
loop:
- show run
- show version
- show interface status
- show license
- name: Send a feedback by email
mail:
host: smtp.gmail.com
port: 587
username: [email protected]
password: sample2
to: [email protected]
subject: '{{ ansible_hostname }} configuration'
body: 'System {{ ansible_hostname }} has been successfully discovered.'
delegate_to: localhost