ERROR! this task 'apt_repository' has extra params - ansible

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'

Related

How do I resolve an error in my yaml file?

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

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

Ansible to install Sublime Text editor in Linux

I am trying to install Sublime Text (on Ubuntu) using Ansible. Here is the basic Ansible playbook I am trying to use for accomplishing this, based on:
bash commands here to install Sublime Text
Ansible docs apt-repository and apt-key
---
- hosts: all
vars:
- my_repos:
- ppa: https://download.sublimetext.com/
- ppa: [arch=amd64] http://dl.google.com/linux/chrome/deb/
- my_pkgs:
- sublime-text
- google-chrome-stable
tasks:
- Install GPG key
name: install GPG key for SubLimeText
???????
- name: Add specified repositories into sources list using specified filename
apt_repository: repo=deb {{ item }} stable main
state=present
filename='{{ item }}'
with_items:
- my_repos
- name: Install packages
apt: state=installed pkg={{ item }}
with_items:
- my_pkgs
The first task is to install the GPG key for SublimeText (per the 1st link above). I read the Ansible docs here, but I do not know how to translate that into the SublimeText case.
Questions:
In the SublimeText instructions, step 1: a direct link to the GPG key is specified. They say:
Install the GPG key: https://download.sublimetext.com/sublimehq-pub.gpg
but how do I add this using the Ansible apt_key module?
In step 2: Does the task to use apt_repository correspond to the bash command echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
---
- hosts: all
vars:
- my_pkgs:
- sublime-text
- google-chrome-stable
tasks:
- name: Install GPG key for SubLimeText
apt_key:
url: https://download.sublimetext.com/sublimehq-pub.gpg
state: present
- name: Add specified repositories into sources list using specified filename
apt_repository:
repo: deb {{ item.repo }} {{ item.add }}
state: present
filename: "{{ item.file }}"
with_items:
- repo: https://download.sublimetext.com/
add: apt/stable/
file: sublime
- repo: '[arch=amd64] http://dl.google.com/linux/chrome/deb/'
add: stable main
file: google-chrome
- name: Install packages
apt:
state: installed
pkg: "{{ item }}"
update_cache: yes
with_items:
- "{{ my_pkgs }}"
For Fedora 32, the following works as part of my main.yaml. Of course, remove the ... and edit to your needs :-)
- hosts: fedora
become: true
tasks:
...
- name: Add Sublime repository
yum_repository:
name: sublime-text
description: Sublime Text x86_64 - stable
baseurl: https://download.sublimetext.com/rpm/stable/x86_64
enabled: yes
gpgcheck: yes
- name: Add Sublime Text
dnf:
name: sublime-text
state: present
...

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.

How to do best organization of install and delete in the same ansible playbook?

I try to figure how I must to construct an ansible playbooks where I can do some action and undo them (I can install or remove same packages; place file or remove this file).
If I create two ansible playbooks: delete.yml and install.yml. There are may be problem's like:
I added to installation someting, but don't change deletion
Example:
install.yml:
---
- name: Add x2go repository
apt_repository: repo='deb http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
apt_repository: repo='deb-src http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
- name: Install x2go application
apt: name=x2goserver update_cache=yes state=present
apt: name=x2goserver-xsession update_cache=no state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
delete.yml:
---
- name: Add x2go repository
apt_repository: repo='deb http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=absent
apt_repository: repo='deb-src http://ppa.launchpad.net/x2go/stable/ubuntu precise main' state=present
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
- name: Install x2go application
apt: name=x2goserver update_cache=yes state=absent
apt: name=x2goserver-xsession update_cache=no state=absent
when: ansible_os_family == "Debian"
tags:
- remote-access-x2go
This is a very interesting idea. I have personally never tried the 'undoing' workflow, but I can see the nice things about this idea and would like to use it sometime. Here is what I would do.
In my ansible-role/defaults/main.yml I would define a variable flag
# defaults file for ansible-role
flag_undo: false
In my ansible-role/tasks/main.yml I would have
- name: task foo bar
command: falana dhimaka
- name: undoing task foo bar
command: undo falana dhimaka
when: flag_undo=true
So by default our flag is always false. So when installing things I would us the first command below to run my plays. And to uninstall I would use the second command.
ansible-playbook foo-play.yml
ansible-playbook foo-play.yml --extra-vars "flag_undo=true"
One approach that I use in some cases is to simply have lists of packages that you want installed and lists you want removed, then iterate over each list. I use this basic method not only for packages but other things as well, like users, groups, etc. For example, I have a "packages" role that has the following files in it:
vars/main.yml:
---
installed_system_packages:
- telnet
- screen
- postfix
latest_system_packages:
- glibc
removed_packages:
- sendmail
tasks/main.yml:
---
- name: Install system packages (latest)
yum: pkg={{ item }} state=latest
with_items: latest_system_packages
- name: Install system packages
action: yum pkg={{ item }} state=installed
with_items: installed_system_packages
- name: Remove unwanted packages
action: yum pkg={{ item }} state=removed
with_items: removed_packages
This way, if I decide that I no longer want a package like telnet installed I can just move it from installed_system_packages to removed_packages. Or if I want to ensure I'm running the latest version of screen I would simply move it to the latest_system_packages list. Then it's just a matter of re-running the role to have the changes applied.

Resources