Review this YAML code

This is for Task 2 in this lab Project - Playbook - KodeKloud

Can someone explain to me why the below code does not pass the check? I know it’s not the same exact syntax as the solution but I don’t understand why this doesn’t work.

#install dependencies

- name: install dependencies
  hosts: all
  become: true
  tasks:
  - name: install dependencies
    yum:
     name:
     - firewalld
     - libselinux-python
     - libsemanage-python
     state: installed
#install and configure MariaDB
  - name: install and config MariaDB
    yum: name=mariadb state=installed
    service: name=mariadb state=started enabled=true
    service: name=firewalld state=started enabled=true
    firewalld: port=3306/tcp state=enabled permanent=true immediate=true
    copy: src=/files/my.cnf dest=/etc/

Hi @cjb
The playbooks fail due to :

  • The lamp stack should only be deployed on lamp-db hosts
  • You install only MariaDB no MySQL-python package
  • The correct name for mariadb is mariadb-server
  • The path you provide for my.cnf is not correct just provide the relative path files/my.cnf
  • At the end you have a syntax error

see below sample

# Install on all hosts
- name: install dependencies
  hosts: all
  become: true
  tasks:
  - name: install dependencies
    yum:
     name:
     - firewalld
     - libselinux-python
     - libsemanage-python
     state: installed

# Install only on lamp-db
- name: Deploy Lamp stack
  hosts: lamp-db
  become: true
  tasks:
  - name: install and config MariaDB
    yum: name=mariadb-server state=installed
  - yum: name=MySQL-python state=installed
  - service: name=mariadb state=started enabled=true
  - service: name=firewalld state=started enabled=true
  - firewalld: port=3306/tcp state=enabled permanent=true immediate=true
  - copy: src=files/my.cnf dest=/etc/

NB :
- The syntax you provide is correct but unreadable, you can use the syntax provided on the solution
- the inventory file gives some variable, you can use it on the YAML file like mysql_port

Hi
Thank you for replying
My next question…Is it necessary to put the hyphen before each module name, like you have it below?
image

Would it still work without the hyphens? Is it not possible to have multiple tasks run without hyphens?

Hi @cjb

The task is a list, so each element should have a hyphen without it, he became a dictionary and you’ll have syntax error.

You can go through this document to have a good visibility of YAML syntax YAML Syntax — Ansible Documentation

You can go through the chapter provide by KodeKloud on Ansible for beginners course

Thanks
I also want to know why in some of the labs do they use the following syntax
copy:
src: /example
dest: /example

and in others they use
copy: src=/example dest=/example

sometimes i have used the first one and it did not work

Hi,
Both are same, just the second one is inline syntax.
For the first one, be sure that you respect the indentation :

copy:
  src: /example
  dest: /example

src and dest should be aligned

1 Like

thank you for your help