Indentation error while running ansible playbook - ansible

I'm a newbee to ansible. While running this playbook, getting below error. Although I searched for the solution over the web I was unable to get any help specific to my problem. The problem seems to be with "notify:" syntax but not sure what exactly. Please can anyone help me in finding where is the mistake.
Ansible playbook -
---
- hosts: droplets
remote_user: root
tasks:
- name: Check if service httpd running
service: name=httpd state=running
notify:
- start apache service
handlers:
- name: start apache
service: name=httpd state=started
...
Output:
root#zarvis:/home/luckee/ansible# ansible-playbook servicechk.yml -f 2
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/luckee/ansible/servicechk.yml': line 10, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- start apache service
^ here
Any help would be great !

1.) First line. There should be no whitespace characters before three dashes --- which is called also Document Boundary Marker:
A line beginning with “---” may be used to explicitly denote the beginning of a new YAML document.
2.)
notify:
- start apache service
replace with
notify:
- start apache
since you declaring start apache handler.

Correct syntax would be:
hosts: host1
remote_user: vagrant
tasks:
name: Check if service httpd running
service: name=httpd state=started
notify:
start_apache
handlers:
name: start apache
service: name=httpd state=started
...

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:

This task (name) has extra params, which is only allowed

Having an issue with ansible.builtin.shell and ansible.builtin.command. Probably not using them right, but usage matches the docs examples.
Ansible version 2.10.3
In roles/rabbitmq/tasks/main.yml
---
# tasks file for rabbitmq
# If not shut down cleanly, the following will fix:
# systemctl stop rabbitmq-server
- name: Stop RabbitMQ service
ansible.builtin.service:
name: rabbitmq-server
state: stopped
become: yes
# rabbitmqctl force_boot
# https://www.rabbitmq.com/rabbitmqctl.8.html
# force_boot Ensures that the node will start next time, even if it was not the last to shut down.
- name: Force RabbitMQ to boot anyway
ansible.builtin.shell: /usr/sbin/rabbitmqctl force_boot
# systemctl start rabbitmq-server
- name: Stop RabbitMQ service
ansible.builtin.service:
name: rabbitmq-server
state: started
become: yes
Resulting in the following error:
ERROR! this task 'ansible.builtin.shell' has extra params, which is only allowed in the following modules: shell, command, ansible.windows.win_shell, ...
The error appears to be in '.../roles/rabbitmq/tasks/main.yml': line 15, > column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
# force_boot Ensures that the node will start next time, even if it was not the last to shut down.
- name: Force RabbitMQ to boot anyway
^ here
I've tried ansible.builtin.command, both with and without the cmd: parameter.
What don't I understand about the usage?
Try this:
- name: Force RabbitMQ to boot anyway
command: "/usr/sbin/rabbitmqctl force_boot"
register: result
ignore_errors: True
I basically took out the ansible.builtin.. It works for me.
register captures the output into a variable named result.
ignore_errors is useful so that if an error occurs Ansible will not stop.
You can output that variable with:
- debug: var=result
Update your Ansible to the latest version, its a bug https://github.com/ansible/ansible/pull/71824
Try putting "" around your command i.e.
ansible.builtin.shell: "/usr/sbin/rabbitmqctl force_boot"

Issue with handler restarting service

I have an issue where I was handed a gaming service from a dev group in Bulgaria that was coded and built last fall. the only documentation I have for the platform which is a video showing one of the devs running the playbooks to build the env. the video doesnt show version of ansible that is being used. I am trying to run playbooks on ubuntu 16.04 and ansible 2.0.0.2. I am trying to execute the following:
- hosts: nler-example-nxx
user: ubuntu
tasks:
- include: ../../templates/nler-nxx.yml
- name: change nodes ip in nxx config
replace:
path: /www/nler-nxx/conf/nxx.properties
regexp: '(\s+)nxx.defaultPeers=(.*)$'
replace: '\1nxt.defaultPeers=52.202.223.114; 35.173.242.207; 34.238.136.137; 54.164.46.62; 54.86.17.225;'
notify:
- restart nxx service
handlers:
- name: restart nxx service
become: yes
systemd: name=nxx state=restarted
And get this error:
ERROR! no action detected in task
The error appears to have been in '/etc/ansible/playbook/niler/example/nxx.yml': line 15, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
handlers:
- name: restart nxx service
^ here
In doing some research I am thinking this is a version conflict issue but no way to confirm what version they were using.
Indentation is wrong. Should be:
tasks:
- include: ../../templates/nler-nxx.yml
...
.
handlers:
- name: restart nxx service
...

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

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

Simple ansible playbook syntax error (YAML)

I've just started ansible and have created a simple playbook to deploy nginx on a target server. The YAML playbook file (myplaybook.yml) looks like this:-
- name: Configure webserver with nginx
hosts: webservers
sudo: True
tasks:
- name: install nginx
- apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088
When I execute:-
$ ansible-playbook myplaybook.yml
I get:-
ERROR: Syntax Error while loading YAML script, nginx-ansible.yml
Note: The error may actually appear before this position: line 7, column 23
- apt: name=nginx update_cache=yes
environment:
^
I don't see why this error occurs - the hosts file contains the [webservers] section OK - can anyone help?
Thanks!
You've got a couple problems with your YAML. First, - name and - apt shouldn't both have the - prefix. That's making Ansible think you have one task with a name of install nginx but no module or anything else associated with it, then you have a second task with no name but invokes the apt module.
The second problem is indentation. You have an extra space in front of the word environment that makes YAML think you're starting a new child element and not just adding attributes to the current task. So your entire task should look something like this (and remember that spacing is critical):
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088

Resources