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

John Mahowald
  • 32,050
  • 2
  • 19
  • 34

1 Answers1

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.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
  • 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. – Prabal Pratap Singh Apr 18 '22 at 12:49