yaml syntax is extremely annoying

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.

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.

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

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

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?

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.