# yaml syntax is extremely annoying

**URL:** <https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380>\
**Category:** Ansible Project\
**Created:** [May 2, 2017, 3:58pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380 "2017-05-02T15:58:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [May 2, 2017, 3:58pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/1 "2017-05-02T15:58:43Z")

</div>

i had a play like this

- name: Install jdk  
become: yes  
become\_method: sudo  
become\_user: root  
command: rpm -ivf /tmp/kafka-src/{{ jdkInstaller }}  
when: “‘oraclejdk’ not in is\_jdkInstalled.stdout”

i wanted to add directory delete, directory create and file copy

- name: Install jdk  
become: yes  
become\_method: sudo  
become\_user: root  
shell: rm -rf /tmp/kafka-src

- file:  
path=/tmp/kafka-src state=directory  
state: directory

- copy:  
src: “{{ BuildSrvPkgLoc }}/{{ jdkInstaller }}”  
dest: /tmp/kafka-src/  
command: rpm -ivf /tmp/kafka-src/{{ jdkInstaller }}  
when: “‘oraclejdk’ not in is\_jdkInstalled.stdout”

it did not like the -file or -copy

then i tried

- name: Install jdk  
become: yes  
become\_method: sudo  
become\_user: root  
shell: rm -rf /tmp/kafka-src  
file: path=/tmp/kafka-src state=directory  
copy: src={{ BuildSrvPkgLoc }}/{{ jdkInstaller }} dest=/tmp/kafka-src/

command: rpm -ivf /tmp/kafka-src/{{ jdkInstaller }}

#when: is\_installed.rc == 1  
when: “‘oraclejdk’ not in is\_jdkInstalled.stdout”

this says syntax error at -name now.

can you please help me understand where the syntax error is? Is there a way to get more useful syntax errors? Is there a syntax checker or synta helper online?

I spend more time fixing syntax errors than actually creating play logic. Please help. thanks.

---

<div class="post-metadata">

**Author:** ![Allen\_Fisher1](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Allen\_Fisher1](https://forum.ansible.com/u/Allen_Fisher1)\
**Post date:** [May 2, 2017, 4:20pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/2 "2017-05-02T16:20:43Z")

</div>

The your directory operations would each be their own play.

- name install jdk  
#stuff
- name: remove kafka src directory  
shell: 

-name: create kafka src directory  
file:   
… etc.

Ansible plays do one thing at a time.

---

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [May 2, 2017, 4:49pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/3 "2017-05-02T16:49:32Z")

</div>

Accodring to online yaml validator, the below passes syntax validation and yet ansible throws an error.

- name: Install jdk  
become: yes  
become\_method: sudo  
become\_user: root  
shell: rm -rf /tmp/kafka-src  
file: path=/tmp/kafka-src state=directory  
copy: src={{ BuildSrvPkgLoc }}/{{ jdkInstaller }} dest=/tmp/kafka-src/

command: rpm -ivf /tmp/kafka-src/{{ jdkInstaller }}  
when: “‘oraclejdk’ not in is\_jdkInstalled.stdout”

this is my entire playbook

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 2, 2017, 4:58pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/4 "2017-05-02T16:58:59Z")

</div>

A task can only include a call to a single module. They have a 1 to 1 relationship.

You would need to break up `Install kafka` up into 4 tasks, 1 for each:

shell, file, copy, command

---

<div class="post-metadata">

**Author:** ![madhan0618](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@madhan0618](https://forum.ansible.com/u/madhan0618)\
**Post date:** [May 2, 2017, 5:20pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/5 "2017-05-02T17:20:17Z")

</div>

Splitting it seems to work. thanks a ton. I have other playbooks where i call more than one module from a task and wonder how that works

here is another play book that works

- name: get list of zookeeper hostnames  
hosts: zookeeperServers  
tasks:  
#- debug: msg=“{{ inventory\_hostname }}”  
#- debug: msg=“{{ play\_hosts.index(inventory\_hostname) }}”

- local\_action: ‘lineinfile create=yes dest=./group\_vars/zookHostnames.yaml  
line=“zkhost{{ play\_hosts.index(inventory\_hostname) }}: {{ inven  
tory\_hostname }}”’

- name: get list of kafka hostnames  
hosts: kafkaServers  
tasks:  
#- debug: msg=“{{ inventory\_hostname }}”  
#- debug: msg=“{{ play\_hosts.index(inventory\_hostname) }}”

- local\_action: ‘lineinfile create=yes dest=./group\_vars/kafkaHostnames.yaml  
line=“kfkahost{{ play\_hosts.index(inventory\_hostname) }}: {{ inv  
entory\_hostname }}”’

#- name: echo var

# hosts: kafkaServers

# tasks:

# - debug: msg=“{{ kfkaCnt }}”

- name: update server.properties and kafka service on kafka servers  
hosts: kafkaServers  
become: yes  
become\_method: sudo  
become\_user: root  
tasks:
- fail: msg=“Please pass kfkaCnt variable from command line”  
when: kfkaCnt is not defined
- fail: msg=“Please pass zkLoc variable to specify local or remote zookeeper”  
when: zkLoc is not defined
- set\_fact:  
zLoc: “none”
- set\_fact:  
zLoc: “localZookService”  
when: zkLoc == “local”
- set\_fact:  
zLoc: “remoteZookService”  
when: zkLoc == “remote”
- set\_fact:  
nodeCnt: “none”
- set\_fact:  
nodeCnt: “3node”  
when: kfkaCnt == “3”
- set\_fact:  
nodeCnt: “5node”  
when: kfkaCnt == “5”
- fail: msg=“Please pass valid kfkaCnt variable from command line”  
when: kfkaCnt == “none”
- fail: msg=“Please pass valid zkLoc variable local or remot”  
when: zLoc == “none”
- include\_vars:  
dir: ‘./group\_vars’  
depth: 1
- template:  
src: ./files/{{ nodeCnt }}/kfhost{{ play\_hosts.index(inventory\_hostname) }}.j2  
dest: /app/bin/kafka/config/server.properties
- copy:  
src: ./files/common/{{ zLoc }}  
dest: /usr/lib/systemd/system/kafka.service
- command: systemctl daemon-reload

as you can see i have a template, copy and command module among others. How is this working?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 2, 2017, 5:23pm UTC](https://forum.ansible.com/t/yaml-syntax-is-extremely-annoying/24380/6 "2017-05-02T17:23:32Z")

</div>

The - is the indicate for a task, tasks: is a 'list of tasks', - is  
'list item', you do NOT have multiple modules/actions per task, you  
have 1 per task.
