以下是我的 yaml 文件,用於 cisco 設備的 ansible 配置。在 /etc/ansible/hosts 中,我還編輯了主機檔案以反映我的 Amazon EC2 Ami 實例,如下所示
[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
首先你需要修復你的純yaml。
您需要注意與全域任務相關的選項,或者是特定模組的參數:它們應該正確縮排以反映其正確的範圍。
您需要確保閱讀有關要使用的模組的文檔。例如,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