How do I resolve an error in my yaml file? - ansible

I'm trying to install docker on a node using an ansible playbook, but I keep getting an error. Here's the playbook
---
- host: all
become: yes
become_user: root
tasks:
- name Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository
apt_repository:
repo: deb [arch=and64] https://download.docker.com/linux/ubuntu {{ansible_distributionrelease}} stable
- name: Install list of packages
apt:
name: "{{item}}"
state: installed
update_cache: yes
with_items:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- docker-ce
And here's the error message
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded
Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to be in '/home/ubuntu/docker.yml': line 7, column 12, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name Add Docker GPG key
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
^ here
What am I missing?

I realized my mistake. I was missing a colon on this line after 'name'
- name: Add Docker GPG key

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

ERROR! this task 'apt_repository' has extra params

For the first time I am trying to use Ansible . When I tried to run a playbook I got this error:
ERROR! this task 'apt_repository' has extra params, which is only allowed in the following modules: command, shell, script, include, include_vars, add_host, group_by, set_fact, raw, meta
The error appears to have been in '/home/prism/Desktop/ansible/basic_package/main.yml': line 9, column 5, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: "Add Webupd8 ppa for youtube-dl"
^ here
main.yml :
---
- hosts: all
remote_user: root
tasks:
- name: "Upgrade the whole system"
apt: upgrade=dist update_cache=yes
- name: "Add Webupd8 ppa for youtube-dl"
apt_repository: repo ='ppa:nilarimogard/webupd8'
- name: "Install basic package"
apt: name={{ item }} state=installed
with_items:
- libffi-dev
- vnstat
- youtube-dl
- finger
- htop
- python3-dev
- axel
- curl
- fail2ban
- python-dev
- sendmail
- git
- python-software-properties
- software-properties-common
- python-pip
- nethogs
- unzip
- nmap
Looks like you have an extra space after repo parameter in the apt_repository task. Use the below code:
apt_repository: repo='ppa:nilarimogard/webupd8'

ERROR: apt is not a legal parameter of an Ansible Play

I'm getting the following error when trying to run a YML file:-
user#ubuntuA:~$ ansible-playbook -i hostfile setup.yml
ERROR:
apt is not a legal parameter of an Ansible Play
Ansible version: 1.9.2
yml-file:-
---
- name: Install MySQL server
apt: name=mysql-server state=latest
- name: Install Apache module for MySQL authentication
apt: name=libapache2-mod-auth-mysql state=latest
- name: Install MySQL module for PHP
apt: name=php5-mysql state=latest
Your yml file should look something like this:
---
- hosts: all
become: yes
tasks:
- name: Install packages
apt:
name:
- mysql-server
- libapache2-mod-auth-mysql
- php5-mysql
state: latest
cache_valid_time: 3600 # update cache if more than an hour old
You are trying to execute your setup.yml file directly with ansible-playbook. As #smiler171 mentioned in his answer, correct format for this is the following:
---
- hosts: all
tasks:
- name: Install MySQL server
apt: name=mysql-server state=latest
- name: Install Apache module for MySQL authentication
apt: name=libapache2-mod-auth-mysql state=latest
- name: Install MySQL module for PHP
apt: name=php5-mysql state=latest
Your current file format is for imports and includes. It is useful if you want to reuse tasks from setup.yml somewhere else. In this case you can create another file (let's say playbook.yml) like that:
---
- hosts: all
tasks:
- import_tasks: setup.yml
and run it:
ansible-playbook -i hostfile playbook.yml
Usually, that means that your playbook yml file does not comply with yml syntax. Check for spaces , hyphen etc. Take a look at existing working yml files, like the one pasted by smiller171 in the above answer. I also had a similar error , turned out that my syntax was incorrect.

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