0

The following is my yaml file for a ansible configuration of a cisco device. In /etc/ansible/hosts I have also edited the hosts file to reflect my Amazon EC2 Ami instance as can be seen below

[ec2-instances]
ec2-54-152-72-23.compute-1.amazonaws.com

Inventory file ^

YAML File Below

---
-  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   
...

I get the following error message when trying to run the following. Any idea why this is happening? I have switched out what is seen in the initial .yaml file provided with [ec2-instances] and there is still no difference in runtime result.

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 Answer 1

0

First you need to fix your pure yaml.

You need to pay attention to options which are related to a task globally, or are parameters of a particular module: They should be indented correctly to reflect their correct scope.

You need to make sure you read the doc about the modules you want to use. For example, cli_command does accept directly a string but needs an explicit command option and possibly others. It will not accept a list so will have to loop other your commands.

Lastly there are some oddities you should check:

  • You are giving a single remote host as target for your play (that's ok) but listing connection local. If this is really a remote host, this cannot work as you will need to ssh into it.
  • You are using (what I believe is) a task option save_when. I think I understant it is at the end of the file by error and should be used with the cli_command module call. Meanwhile it is not listed as a parameter of this module (but is part of other relevant cisco ios modules

At this point, I don't really get what you want to do exactly so I can only fix your playbook with what I think is correct so you can try to move forward.

---
- 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .