cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Starfighter Starfighter
Starfighter
  • 2,664 Views

YAML syntax

Jump to solution

Hello all,

After running the command "ansible-doc yum", toward the end of the output, there are some nice example snippets regarding the use of the "yum" module in a playbook.

One of the examples is the following:

- name: Download the nginx package but do not install it
  yum:
      name:
          - nginx
      state: latest 

 

What's prompting a question from me is the "nginx" package name being shown as a list item (- nginx).  Is there any functional difference between the package name being
shown in the form of:

       name:  nginx

versus 

       name:   
            -  nginx

 

If so, what do I gain by showing the package name as a list item?   Another example
in that same output (ansible-doc  yum) is the following:

- name: Install a list of packages
  yum:
      name:
           - nginx
           - postgresql
           - postgresql-server
      state: present

I can connect with that example,  There are three packages to be operated on, and it makes sense (to me) to use a "list".  

Okay, a little help here.

Hope everyone is safe and well!!!

 

Trevor "Red Hat Evangelist" Chandler
Labels (2)
0 Kudos
2 Solutions

Accepted Solutions
Alexandre
Starfighter Starfighter
Starfighter
  • 2,646 Views

Hi

No problem to use one line form here:

- name: Install a list of packages
  yum:
    name: [nginx,postgresql,postgresql-server]
    state: present

View solution in original post

Metsie
Mission Specialist
Mission Specialist
  • 2,574 Views

Hi Trevor,

AFAIK, there should not be any significant difference. The yum module supports both a string and a list. So you can choose to use the short syntax you provided here with no problems at all.

It is not recommended however to loop the yum module.

- name: install packages
  yum:
    name: "{{ item }}"
    state: latest
  loop:
    - nginx
    - postgresql
    - postgresql-server

Would run the yum module once for each package in the list (so three times in this example), where

- name: Install packages
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: latest

Would run the yum module a single time to install all packages at once.

View solution in original post

4 Replies
Alexandre
Starfighter Starfighter
Starfighter
  • 2,647 Views

Hi

No problem to use one line form here:

- name: Install a list of packages
  yum:
    name: [nginx,postgresql,postgresql-server]
    state: present

Trevor
Starfighter Starfighter
Starfighter
  • 2,629 Views

Thanks Alexandre for your reply.  Your information has advanced my understanding of YAML because I was not aware of the form that you provided.  Thank you for that.

I'm still not clear if there is something different in the funtionality of:

name:  nginx

      versus 

 name:   
     -  nginx  

 

Again, thank you advancing my YAML knowledge.

 

 

 

 

Trevor "Red Hat Evangelist" Chandler
Metsie
Mission Specialist
Mission Specialist
  • 2,575 Views

Hi Trevor,

AFAIK, there should not be any significant difference. The yum module supports both a string and a list. So you can choose to use the short syntax you provided here with no problems at all.

It is not recommended however to loop the yum module.

- name: install packages
  yum:
    name: "{{ item }}"
    state: latest
  loop:
    - nginx
    - postgresql
    - postgresql-server

Would run the yum module once for each package in the list (so three times in this example), where

- name: Install packages
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: latest

Would run the yum module a single time to install all packages at once.

Trevor
Starfighter Starfighter
Starfighter
  • 2,555 Views

Hello Metsie,

Many thanks for your response to my query, and for the
additional information as well.

Hope you're safe and well!!!

Trevor

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.