Score:0

Ansible playbook error

cn flag

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 avatar
cn flag
I have edited to add code blocks. On Stack Exchange, YAML will be formatted as Markdown and be unreadable.
Score:0
cn flag

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.

Prabal Pratap Singh avatar
cn flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.