yaml syntax is extremely annoying

59 views
Skip to first unread message

madha...@gmail.com

unread,
May 2, 2017, 11:58:44 AM5/2/17
to Ansible Project
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.

madha...@gmail.com

unread,
May 2, 2017, 12:49:32 PM5/2/17
to Ansible Project
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
---
- hosts: kafkaServers

  tasks:
  - name: Include variables
    include_vars:
      dir: './group_vars'
      depth: 1
  - name: Check if jdk is already installed
    shell: "rpm -qa | grep oraclejdk"
    register: is_jdkInstalled
    failed_when: is_jdkInstalled.rc > 1
    changed_when: no

  - name: Check if kafka is already installed
    shell: "rpm -qa | grep kafka"
    register: is_kafkaInstalled
    failed_when: is_kafkaInstalled.rc > 1
    changed_when: no
  #- name: debug
  #  debug: msg="{{ is_installed }}"

  - name: Install jdk
    become: yes
    become_method: sudo
    become_user: root
    shell: rm -rf /tmp/kafka-src
    file: path=/tmp/kafka-src state=directory
#      path: /tmp/kafka-src
#      state: directory
    copy: src={{ BuildSrvPkgLoc }}/{{ jdkInstaller }} dest=/tmp/kafka-src/
#      src: "{{ BuildSrvPkgLoc }}/{{ jdkInstaller }}"
#      dest: /tmp/kafka-src/
    command: rpm -ivf /tmp/kafka-src/{{ jdkInstaller }}
    when: "'oraclejdk' not in is_jdkInstalled.stdout"

  - name: Install kafka
    become: yes
    become_method: sudo
    become_user: root
    shell: rm -rf /tmp/kafka-src
    file:
      path: /tmp/kafka-src
      state: directory
    copy:
      src: "{{ BuildSrvPkgLoc }}/{{ kafkaInstaller }}"
      dest: /tmp/kafka-src/
    command: rpm -ivf /tmp/kafka-src/{{ kafkaInstaller }}
    #when: is_installed.rc == 1
    when: "'kafka' not in is_kafkaInstalled.stdout"

Matt Martz

unread,
May 2, 2017, 12:59:54 PM5/2/17
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0bd9d450-7bba-4bf6-bb8b-5f1c4a32563d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

madha...@gmail.com

unread,
May 2, 2017, 1:20:17 PM5/2/17
to Ansible Project
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?
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Brian Coca

unread,
May 2, 2017, 1:23:41 PM5/2/17
to Ansible Project
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.


----------
Brian Coca

Allen Fisher

unread,
May 2, 2017, 2:33:36 PM5/2/17
to Ansible Project
The your directory operations would each be their own play.

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

-name: create kafka src directory
  file: <command> 
... etc.

Ansible plays do one thing at a time.
Reply all
Reply to author
Forward
0 new messages