ANSIBLE:: Could not resolve the syntax in the following yaml file - ansible

I am learning ansible and this is my YAML file.
---# Outline to playbook translation
- hosts: node1
user: test
sudo: yes
gather_facts: no
tasks:
- name: date time stamp at start
raw: /usr/bin/date > /home/test/playbook_start.log
- name: install apache web server
yum: pkg=httpd state=latest
- name: start the service
service: name=httd state=restarted
- name: verify web service is running or not
command: systemctl status httpd
register: result
- debug: var-result
- name: install client SW telnet
yum: pkg=telnet state=latest
- name: install client pkg VIM
yum: pkg=vim state=latest
and I get this error while running
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/test/Outline/webserver.yml': line 2, column 8, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---# Outline to playbook translation
- hosts: node1
^ here

Try this file:
---
# Outline to playbook translation
- hosts: node1
user: test
sudo: yes
gather_facts: no
tasks:
- name: date time stamp at start
raw: /usr/bin/date > /home/test/playbook_start.log
- name: install apache web server
yum: pkg=httpd state=latest
- name: start the service
service: name=httd state=restarted
- name: verify web service is running or not
command: systemctl status httpd
register: result
- debug: var=result
- name: install client SW telnet
yum: pkg=telnet state=latest
- name: install client pkg VIM
yum: pkg=vim state=latest

The syntax error is because you did not separate the comment on the first line of the file from the preceding token (the directives end marker: ---):
Comments must be separated from other tokens by white space characters.
try:
--- # Outline to playbook translation

Either remove "# Outline to playbook translation" or put it in a next line or give a space after ---

Related

I just tried to write a playbook same error in every playbook

--- # this is first playbook with handler
- host: demo
user: ansible
become: yes
connection: ssh
vars:
pkgname: httpd
task:
- name: install httpd service
action: yum name='{{pkgname}}' state=installed
notify: restart httpd
handler:
- name: restart httpd
action: service name=httpd state=restarted
ERROR! 'task' is not a valid attribute for a Play
The error appears to be in '/home/ansible/handler.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
--- # this is first playbook with handler
- host: demo
^ here
i think it is tasks: (plural), not task:

why playbook is doesn't play?

I have a problem, I try start my playbook and I get an error:
ERROR! 'task' is not a valid attribute for a Play
The error appears to have been in '/root/devops/ansible/playbook2.yml': line 2, column 4, but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: nginx
^ here
my playbook:
- name: nginx
hosts: all
become: yes
task:
- name: install
apt: name=nginx state=latest
- name: start nginx
service: name=nginx.service state=started enabled=yes
What is my mistake?
It is tasks.
- name: nginx
hosts: all
become: yes
tasks:
- name: install
apt: name=nginx state=latest
- name: start nginx
service: name=nginx.service state=started enabled=yes
Refer playbook-language-example for ansible playbook syntax.

Ansible - How to run multiple tasks in a playbook

I'm new to ansible, and am attempting to run multiple tasks to install docker on a particular host group in an ansible playbook.
I have the following playbook...
---
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: yes
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400 #One day
- hosts: loadbalancer
become: yes
tasks:
- name: Install docker packages
apt:
name:
- 'apt-transport-https'
- 'ca-certificates'
- 'curl'
- 'software-properties-common'
state: present
- name: Add Docker official GPG key
apt-key:
url: https://download.docker.com/linux/ubuntu/gpg
This is the error I get when attempting to run the playbook...
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to be in '/home/vagrant/ansible/playbooks/hostname.yml': line 23, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Add Docker official GPG key
^ here
What am I doing wrong here?
Thanks,
It is not apt-key, it is apt_key. Please update and try again

YAML syntax error (Ansible playboook)

I am trying to write a playbook that installs Apache, but I get the below error:
The offending line appears to be:
tasks:
- name: command to install apache
^ here
Here is my YAML code:
---
- hosts: all
tasks:
- name: command to install apache
sudo: yes
yum: name=httpd state=latest
service: name=httpd state=running
What could be wrong here?
You cannot add two actions (modules) to a single task in Ansible.
You need to split yum and service into two tasks.
Also sudo declaration was deprecated long time ago and now become should be used:
---
- hosts: all
tasks:
- name: Ensure apache is installed
become: yes
yum: name=httpd state=latest
- name: Ensure httpd service is running
become: yes
service: name=httpd state=running

ERROR: apt is not a legal parameter of an Ansible Play

I'm getting the following error when trying to run a YML file:-
user#ubuntuA:~$ ansible-playbook -i hostfile setup.yml
ERROR:
apt is not a legal parameter of an Ansible Play
Ansible version: 1.9.2
yml-file:-
---
- name: Install MySQL server
apt: name=mysql-server state=latest
- name: Install Apache module for MySQL authentication
apt: name=libapache2-mod-auth-mysql state=latest
- name: Install MySQL module for PHP
apt: name=php5-mysql state=latest
Your yml file should look something like this:
---
- hosts: all
become: yes
tasks:
- name: Install packages
apt:
name:
- mysql-server
- libapache2-mod-auth-mysql
- php5-mysql
state: latest
cache_valid_time: 3600 # update cache if more than an hour old
You are trying to execute your setup.yml file directly with ansible-playbook. As #smiler171 mentioned in his answer, correct format for this is the following:
---
- hosts: all
tasks:
- name: Install MySQL server
apt: name=mysql-server state=latest
- name: Install Apache module for MySQL authentication
apt: name=libapache2-mod-auth-mysql state=latest
- name: Install MySQL module for PHP
apt: name=php5-mysql state=latest
Your current file format is for imports and includes. It is useful if you want to reuse tasks from setup.yml somewhere else. In this case you can create another file (let's say playbook.yml) like that:
---
- hosts: all
tasks:
- import_tasks: setup.yml
and run it:
ansible-playbook -i hostfile playbook.yml
Usually, that means that your playbook yml file does not comply with yml syntax. Check for spaces , hyphen etc. Take a look at existing working yml files, like the one pasted by smiller171 in the above answer. I also had a similar error , turned out that my syntax was incorrect.

Resources