以下是我的思科设备 ansible 配置的 yaml 文件。在 /etc/ansible/hosts 中,我还编辑了 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