Ansible YAML syntax error in simple playbook - ansible

I have my first playbook and it fails. I assume it's a syntax error but as I'm not a coder I have no idea why YAML fails? Is it to do with spacing?
Here is what I have:
---
- name: Update all packages to the latest version
become: true
apt:
update_cache: yes
upgrade: dist
- name: Remove useless packages from the cache
apt:
autoclean: yes
- name: Remove dependencies that are no longer required
apt:
autoremove: yes
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/pi/playbooks/update-apt.yml': line 3, column 11, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Update all packages to the latest version
become: true
^ here

Firstly: this is not a playbook, because it doesn't contain plays (which must contain hosts declaration), but tasks.
Secondly: your indentation is terribly broken -- it is critical in YAML to keep declarations aligned properly (that said, the error you see is not a YAML syntax error, but an Ansible error resulting from improper data defined in a correctly written YAML file).
If you want to run it locally, it should look more or less like this:
---
- hosts: localhost
connection: local
tasks:
- name: Update all packages to the latest version
become: true
apt:
update_cache: yes
upgrade: dist
autoclean: yes
autoremove: yes

Related

Im running my first ansible playbook and I get an error even though I followed the video exactly

My first Ansible playbook looks like
---
- name: iluvnano
hosts: linux
tasks:
- name: ensure nano is there
apt:
name: nano
state: latest
I get the error
ERROR! conflicting action statements: apt, state
The error appears to be in '/root/test.yml': line 5, column 9, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: ensure nano is there
^ here
According the documentation and examples of apt_module
- name: Install apache httpd (state=present is optional)
apt:
name: apache2
state: present
your indents are incorrect. So the error is just caused by a typo and you may use instead
- name: ensure nano is there
apt:
name: nano
state: latest

Ansible - How to run multiple tasks in a playbook

I'm new to ansible, and am attempting to run multiple tasks to install docker on a particular host group in an ansible playbook.
I have the following playbook...
---
- hosts: all
tasks:
- name: Update and upgrade apt packages
become: yes
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400 #One day
- hosts: loadbalancer
become: yes
tasks:
- name: Install docker packages
apt:
name:
- 'apt-transport-https'
- 'ca-certificates'
- 'curl'
- 'software-properties-common'
state: present
- name: Add Docker official GPG key
apt-key:
url: https://download.docker.com/linux/ubuntu/gpg
This is the error I get when attempting to run the playbook...
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to be in '/home/vagrant/ansible/playbooks/hostname.yml': line 23, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Add Docker official GPG key
^ here
What am I doing wrong here?
Thanks,
It is not apt-key, it is apt_key. Please update and try again

I don't know why I'm getting syntax error while loading yaml

I want to update all software packages in the playbook using the following lines,
---
- name: First play
hosts: all
tasks:
- name: Update all the current software packages on the system
yum: name=* state=latest
but I keep receiving an error says there something wrong in the syntax.
The offending line appears to be:
- name: Update all the current software packages on the system
yum: name=* state=latest
^ here
Your "yum" has an incorrect additional space before it, and "- name" needs to be in line with tasks. Try this:
---
- name: First play
hosts: all
tasks:
- name: Update all the current software packages on the system
yum:
name: *
state: latest

YAML Issue in Ansible 2.3.0

I got this playbook to run on a VM running Ansible 2.3.1, with no errors, however, I am running into problems with the very first character in other versions 2.3.0 and older and I am NOT seeing where I am going wrong.
The format exactly follows the examples given in the documentation for cl-license, and this is pretty vanilla in that there is no logic or conditional statements or anything to this YAML.
---
- hosts: accton_as6712_32x
tasks:
- name: install_license_for_6712
cl_license:
src: "http://10.43.255.182/cumulus/license-4x5712-4x6712.txt"
- hosts: accton_as4610_54
tasks:
- name: install_license_for_4610
cl_license:
src: "http://10.43.255.182/cumulus/license-2x4600.txt"
- hosts: mlnx_x86_MSN2410B
tasks:
- name: install_license_for_2410
cl_license:
src: "http://10.43.255.182/cumulus/license-2x2410.txt"
- hosts: mlnx_x86_MSN2700
tasks:
- name: install_license_for_2700
cl_license:
src: "http://10.43.255.182/cumulus/license-mellanox-demo-2700.txt"
This is the error I get from Jenkins/Ansible:
The offending line appears to be:
---
- hosts: accton_as6712_32x
^ here
And if I use YAML Lint, I get this error:
(<unknown>): did not find expected '-' indicator while parsing a block collection at line 2 column 1
I am fairly new to Ansible and would love if somebody could point out where I am going wrong?
I'm not saying the cl-license module is the wrong way but have you considered using ZTP to install licenses on your Cumulus switches?
By the way, the indentation looks correct but make sure you have actual spaces and not tab characters in your file. Also, check for any odd trailing spaces.

Simple ansible playbook syntax error (YAML)

I've just started ansible and have created a simple playbook to deploy nginx on a target server. The YAML playbook file (myplaybook.yml) looks like this:-
- name: Configure webserver with nginx
hosts: webservers
sudo: True
tasks:
- name: install nginx
- apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088
When I execute:-
$ ansible-playbook myplaybook.yml
I get:-
ERROR: Syntax Error while loading YAML script, nginx-ansible.yml
Note: The error may actually appear before this position: line 7, column 23
- apt: name=nginx update_cache=yes
environment:
^
I don't see why this error occurs - the hosts file contains the [webservers] section OK - can anyone help?
Thanks!
You've got a couple problems with your YAML. First, - name and - apt shouldn't both have the - prefix. That's making Ansible think you have one task with a name of install nginx but no module or anything else associated with it, then you have a second task with no name but invokes the apt module.
The second problem is indentation. You have an extra space in front of the word environment that makes YAML think you're starting a new child element and not just adding attributes to the current task. So your entire task should look something like this (and remember that spacing is critical):
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
environment:
http_proxy: myproxy.com:8088
https_proxy: myproxy.com:8088

Resources