# Ansible playbook   yaml syntax errors

**URL:** <https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002>\
**Category:** Ansible Project\
**Created:** [November 11, 2017, 11:14pm UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002 "2017-11-11T23:14:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 11, 2017, 11:14pm UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002/1 "2017-11-11T23:14:30Z")

</div>

Hi all,

I am new to Ansible learning and I start practicing yaml playbook writings.  
Though I am able to have a program with correct indent, ansible-playbook still pointing errors. I have no programming experience than bash scripting.

Could someone highlight, what I am missing.  
Any guidance to do a search here before posting will be much appreciated.

Thanks

`

[student@mgmvm1 ~]$ ansible --version  
ansible 2.3.1.0  
config file = /home/student/ansible.cfg  
configured module search path = Default w/o overrides  
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]  
[student@mgmvm1 ~]$ python -c ‘import yaml, sys; print yaml.load(sys.stdin)’ \< test.yml  
[{‘become’: True, ‘tasks’: [{‘block’: [{‘state’: ‘latest’, ‘yum’: None, ‘name’: ‘httpd’}, {‘state’: ‘latest’, ‘yum’: None, ‘name’: ‘firewalld’}]}, {‘block’: [{‘state’: ‘enabled’, ‘permanent’: True, ‘name’: ‘firewalld permits http service’, ‘service’: ‘http’, ‘firewalld’: None, ‘immediate’: True}]}, {‘block’: [{‘state’: ‘started’, ‘enabled’: True, ‘name’: ‘httpd’, ‘service’: None}, {‘state’: ‘started’, ‘enabled’: True, ‘name’: ‘firewalld’, ‘service’: None}]}, {‘block’: [{‘content’: ‘Welcome to the [example.com](http://example.com) intranet!\n’, ‘dest’: ‘/var/www/html/index.html’, ‘copy’: None, ‘name’: ‘test html page’}]}], ‘hosts’: ‘[ctlvm.example.com](http://ctlvm.example.com)’, ‘name’: ‘intranet services’}, {‘tasks’: [{‘url’: ‘[http://ctlvm.example.com](http://ctlvm.example.com)’, ‘status\_code’: 200, ‘name’: ‘connect to intranet’, ‘uri’: None}], ‘hosts’: ‘localhost’, ‘name’: ‘test’}]  
[student@mgmvm1 ~]$ ansible-playbook --syntax-check test.yml  
[WARNING]: While constructing a mapping from /home/student/test.yml, line 7, column 9, found a duplicate dict key (name).  
Using last defined value only.

[WARNING]: While constructing a mapping from /home/student/test.yml, line 13, column 9, found a duplicate dict key (name).  
Using last defined value only.

[WARNING]: While constructing a mapping from /home/student/test.yml, line 28, column 9, found a duplicate dict key (name).  
Using last defined value only.

[WARNING]: While constructing a mapping from /home/student/test.yml, line 34, column 9, found a duplicate dict key (name).  
Using last defined value only.

ERROR! ‘state’ is not a valid attribute for a Task

The error appears to have been in ‘/home/student/test.yml’: line 7, column 9, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- block:
- name: latest httpd version installed  
^ here

[student@mgmvm1 ~]$ cat test.yml

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 12, 2017, 2:55pm UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002/2 "2017-11-12T14:55:14Z")

</div>

> Hi all,
> 
> I am new to Ansible learning and I start practicing yaml playbook  
> writings.  
> Though I am able to have a program with correct indent, ansible-playbook  
> still pointing errors. I have no programming experience than bash scripting.

It's a valid YAML, but not a valid Ansible syntax.  
You need to study the multiple examples in the documentation and pay attention to the indentation level used in them.

> Could someone highlight, what I am missing.  
> Any guidance to do a search here before posting will be much  
> appreciated.

Instead of just giving the answer I going to explain the process of checking it yourself.  
All you task have the same error so I'm only going to take the first two ones.

> &nbsp;&nbsp;- block:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: latest httpd version installed  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yum:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: latest firewalld version is present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yum:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: firewalld  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest

You don't need the block here, block is to grouping task that you need common directives on or you need error handling.

If you check the documentation of the yum module you'll find examples in the bottom[1]  
Look closely at the indentation and you'll see that yours don't match.

Do that for all you task, since they all have incorrect indentation.

[1] [https://docs.ansible.com/ansible/latest/yum\_module.html#examples](https://docs.ansible.com/ansible/latest/yum_module.html#examples)

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 13, 2017, 12:10am UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002/3 "2017-11-13T00:10:46Z")

</div>

Thanks a Lot.

What I did is ,indent the lines as in the exact examples of per the examples of modules yum, copy and uri and it worked .  
This program is an example given for me to learn block and I continue with that . when I learn block completely , I will do the changes as you suggested.

So , was indent was the only problem I had?  
if so , the service module indent is not the exact one in my program but still it works ?

> ## [student@mgmvm1 ~]$ cat test.yml
> 
> - name: intranet services  
> hosts: [ctlvm.example.com](http://ctlvm.example.com)  
> become: yes  
> tasks:
> 
> - block:
> 
> - name: latest httpd version installed  
> yum:  
> name: httpd  
> state: latest
> 
> - name: latest firewalld version is installed  
> yum:  
> name: firewalld  
> state: latest
> 
> - block:
> 
> - name: firewalld permits http service  
> firewalld:  
> service: http  
> permanent: yes  
> state: enabled  
> immediate: yes
> 
> - block:
> 
> - name: httpd enabled and running  
> service:  
> name: httpd  
> enabled: yes  
> state: started
> 
> - name: firewalld is enabled and running  
> service:  
> name: firewalld  
> enabled: yes  
> state: started
> 
> - block:
> 
> - name: test html page  
> copy:  
> content: “Welcome to the [example.com](http://example.com) intranet!\n”  
> dest: /var/www/html/index.html
> 
> - name: test  
> hosts: localhost  
> tasks:
> 
> - name: connect to intranet  
> uri:  
> url: [http://ctlvm.example.com](http://ctlvm.example.com)  
> status\_code: 200  
> [student@mgmvm1 ~]$ ansible-playbook --syntax-check test.yml  
> playbook: test.yml  
> [student@mgmvm1 ~]$

I am assuming that I will follow the below steps of yaml writing . Will it be ok?

write yaml with exact syntax  
indent as exactly as given(example) in each module  
validate the yaml format  
validate the ansible format.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 13, 2017, 3:37pm UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002/4 "2017-11-13T15:37:23Z")

</div>

> So , was indent was the only problem I had?  
> if so , the service module indent is not the exact one in my program  
> but still it works ?

The order of the lines on the same indentation doesn't matter and some directive is optional like the name.  
So these three is correct syntax:

&nbsp;&nbsp;- yum:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest

&nbsp;&nbsp;- name: latest httpd version installed  
&nbsp;&nbsp;&nbsp;&nbsp;yum:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest

&nbsp;&nbsp;- yum:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest  
&nbsp;&nbsp;&nbsp;&nbsp;name: latest httpd version installed

> I am assuming that I will follow the below steps of yaml writing .  
> Will it be ok?
> 
> write yaml with exact syntax  
> indent as exactly as given(example) in each module  
> validate the yaml format  
> validate the ansible format.

Pretty much, when the syntax is correct you have some leeway in the order the lines in the task is written.  
My suggestion is to look at the code of other and you'll see what you think is more readable and stick to that form.

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [November 14, 2017, 4:40pm UTC](https://forum.ansible.com/t/ansible-playbook-yaml-syntax-errors/26002/5 "2017-11-14T16:40:03Z")

</div>

Thanks a Lot.  
Will continue to learn writing yaml …
