I need assistance with vars, lists, and apt

39 views
Skip to first unread message

Kevin Shumaker

unread,
Aug 19, 2022, 9:24:06 AM8/19/22
to Ansible Project
New user of ansible. I've tried several playbooks to use as templates.
My goal is to create a vars.yml with the following:
= = = = =
---
applications:
   - app1
   - app2
   - app3
   - and so on
...
= = = = =
and file inventory contains list of new_machines (with appropriate link in ansible.cfg)

to be used in a playbook thusly:
= = = = =
- hosts: new_machines
  become: true

  vars_files:
    - /opt/ansible/vars.yml

  pre_tasks:
    - name: Update Cache if needed
      apt: update_cache=true cache_valid_time=2600

  tasks:
    - name install standard stuff
      apt:
      name: {{ applications }}
...
= = = = =
but I always get an error:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/opt/ansible/new_pi.yml': line 14, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    - name install standard stuff
      apt:
         ^ here
pointing to the colon after apt. I've tried several different types, but all fail the same way.

I've spent several days, off and on, looking through the docs and suggestions in the docs.ansible site (awesome resource) but my google-fu fails me, and I am looking for suggestions, either for keyword or phrases to seach for, or suggestions on solutions...
Thanks.

Rowe, Walter P. (Fed)

unread,
Aug 19, 2022, 9:50:11 AM8/19/22
to ansible...@googlegroups.com
Does your 'name' line in your apt task needs to be indented?

    - name install standard stuff
      apt:
      name: {{ applications }}

... should be ...

    - name install standard stuff
      apt:
        name: {{ applications }}

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123


--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60f750dd-4fb6-4552-ae3d-1114c0137983n%40googlegroups.com.

Kevin Shumaker

unread,
Aug 19, 2022, 10:16:21 AM8/19/22
to Ansible Project
Wow, thanks for the speedy reply!

Unfortunately for me, that didn't work (adding 2 spaces further indent on the line "     name: {{ applications }} " although it does make sense.
I still get the same error, at the same point. Kinda frustrating when the keyword "name:" can be used for several different functions.

as an aside, and in a different, non-clean, non-easy to maintain, but to prove to me that ansible can work, sorta, I created the playbook with separate install lines for each app in the playbook:

  tasks:
    - name: install app1
      apt: name=app1 state=present
    - name: install app2
      apt: name=app2 state=present

but I'm at 20 apps, and haven't even begun the setup commands, and copy files, yet, and the playbook is becoming unwieldy, so the vars list setup would be of great assistance...

Thanks again for the speedy reply, though.

Todd Lewis

unread,
Aug 19, 2022, 10:47:31 AM8/19/22
to Ansible Project
"- name install standard stuff" should be "- name: install standard stuff".
You're missing a colon.

Rowe, Walter P. (Fed)

unread,
Aug 19, 2022, 10:47:58 AM8/19/22
to ansible...@googlegroups.com
You specify the what. You don't specify the action. You need a state (ie present, absent, etc).

    - name install standard stuff
      apt:
        name: {{ applications }}
        state: present

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
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-proje...@googlegroups.com.

Kevin Shumaker

unread,
Aug 19, 2022, 11:08:47 AM8/19/22
to Ansible Project
Ding! Ding! Ding!  I need to get my eyes checked... That was the issue! Missing the ':'. The list works as expected, now.
I use Pi-Gen to create my custom OS, but I think Ansible is going to be my replacement, as it does all that Pi-Gen does, but allows me to do it to any of my Linux machines.

Thank you for your rapid response. My hair thanks you, too...

Kevin Shumaker

unread,
Aug 19, 2022, 11:29:38 AM8/19/22
to Ansible Project
Thank you sir. I am not sure why, but it works with or without the status: line. Todd found what was breaking my playbook, though. I did add the status: line just to be consistent, and for readability in the future.

I appreciate both of your assistance, and I hope to contribute in the future.
Reply all
Reply to author
Forward
0 new messages