0

Hi I have just started learning ansible. I am trying to write playbook as:

vpc-setup.yml which including

hosts: localhost
connection: local
gather_facts: False
tasks:
 - name: Import VPC Variables
   include_vars: vars/vpc_setup.md

 - name: Create vprofile VPC
   ec2_vpc_net:
    name: "{{vpc_name}}"
    cidr_block: "{{vpcCidr}}"
    region: "{{region}}"
    dns_hostnames: yes
    tenancy: default
    state: "{{state}}"
    register: vpcout

the error is coming in 1st line:

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
ERROR! A playbook must be a list of plays, got a <class 'ansible.parsing.yaml.objects.AnsibleMapping'> instead

The error appears to be in '/home/ubuntu/ansible-aws-vpc/vpc-setup.yml': line 1, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


hosts: localhost
^ here

Kindly help

1
  • I have edited to add code blocks. On Stack Exchange, YAML will be formatted as Markdown and be unreadable. Apr 18, 2022 at 2:21

1 Answer 1

0

An Ansible playbook is one YAML document containing a list of plays, possibly more than one. Because it is a list, the outer part of the document needs YAML list notation, so -.

- name: VPC for thing
  hosts: localhost
  gather_facts: False
  tasks:

The rest of the play follows.

More examples in Ansible's guide introducing playbooks. Pay close attention to indentation, and the outer - indicating a YAML list.


Bonus tips:

name keyword on plays documents a purpose. Keep name short, 50 characters or less.

Delete connection: local at the play level. If run against a pattern with multiple hosts, it will run many times, which is unlikely what you want. Implicit localhost is already local, or you can set the connection plugin per host or group at the inventory level.

1
  • Thanks John. I will try what you explained. Can we have a direct communication. I am eager to learn and i really need someone who can guide. Hope you understand. Apr 18, 2022 at 12:49

You must log in to answer this question.

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