Ansible - How to run multiple tasks in a playbook - ansible

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

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

YAML syntax error (Ansible playboook)

I am trying to write a playbook that installs Apache, but I get the below error:
The offending line appears to be:
tasks:
- name: command to install apache
^ here
Here is my YAML code:
---
- hosts: all
tasks:
- name: command to install apache
sudo: yes
yum: name=httpd state=latest
service: name=httpd state=running
What could be wrong here?
You cannot add two actions (modules) to a single task in Ansible.
You need to split yum and service into two tasks.
Also sudo declaration was deprecated long time ago and now become should be used:
---
- hosts: all
tasks:
- name: Ensure apache is installed
become: yes
yum: name=httpd state=latest
- name: Ensure httpd service is running
become: yes
service: name=httpd state=running

ANSIBLE:: Could not resolve the syntax in the following yaml file

I am learning ansible and this is my YAML file.
---# Outline to playbook translation
- hosts: node1
user: test
sudo: yes
gather_facts: no
tasks:
- name: date time stamp at start
raw: /usr/bin/date > /home/test/playbook_start.log
- name: install apache web server
yum: pkg=httpd state=latest
- name: start the service
service: name=httd state=restarted
- name: verify web service is running or not
command: systemctl status httpd
register: result
- debug: var-result
- name: install client SW telnet
yum: pkg=telnet state=latest
- name: install client pkg VIM
yum: pkg=vim state=latest
and I get this error while running
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/home/test/Outline/webserver.yml': line 2, column 8, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---# Outline to playbook translation
- hosts: node1
^ here
Try this file:
---
# Outline to playbook translation
- hosts: node1
user: test
sudo: yes
gather_facts: no
tasks:
- name: date time stamp at start
raw: /usr/bin/date > /home/test/playbook_start.log
- name: install apache web server
yum: pkg=httpd state=latest
- name: start the service
service: name=httd state=restarted
- name: verify web service is running or not
command: systemctl status httpd
register: result
- debug: var=result
- name: install client SW telnet
yum: pkg=telnet state=latest
- name: install client pkg VIM
yum: pkg=vim state=latest
The syntax error is because you did not separate the comment on the first line of the file from the preceding token (the directives end marker: ---):
Comments must be separated from other tokens by white space characters.
try:
--- # Outline to playbook translation
Either remove "# Outline to playbook translation" or put it in a next line or give a space after ---

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