why playbook is doesn't play? - ansible

I have a problem, I try start my playbook and I get an error:
ERROR! 'task' is not a valid attribute for a Play
The error appears to have been in '/root/devops/ansible/playbook2.yml': line 2, column 4, but may be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: nginx
^ here
my playbook:
- name: nginx
hosts: all
become: yes
task:
- name: install
apt: name=nginx state=latest
- name: start nginx
service: name=nginx.service state=started enabled=yes
What is my mistake?

It is tasks.
- name: nginx
hosts: all
become: yes
tasks:
- name: install
apt: name=nginx state=latest
- name: start nginx
service: name=nginx.service state=started enabled=yes
Refer playbook-language-example for ansible playbook syntax.

Related

I just tried to write a playbook same error in every playbook

--- # this is first playbook with handler
- host: demo
user: ansible
become: yes
connection: ssh
vars:
pkgname: httpd
task:
- name: install httpd service
action: yum name='{{pkgname}}' state=installed
notify: restart httpd
handler:
- name: restart httpd
action: service name=httpd state=restarted
ERROR! 'task' is not a valid attribute for a Play
The error appears to be in '/home/ansible/handler.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
--- # this is first playbook with handler
- host: demo
^ here
i think it is tasks: (plural), not task:

How to fix the ansible playbook error "Unsupported parameters for (systemd) module: enable Supported parameters?

I am trying to install Apache 2, PHP on Ubuntu machine using the ansible-playbook.
I am getting the following error
Error after executing playbook
fatal: [18.220.215.181]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (systemd) module: enable Supported parameters include: daemon_reexec, daemon_reload, enabled, force, masked, name, no_block, scope, state, user"}The ansible playbook is as follows---
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enable: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
- name: restart apache2 afetr restart
service:
name: apache2
enable: yes
state: restarted
`
The right parameter is enabled (not enable) in your service tasks.
- name: restart apache2 afetr restart
service:
name: apache2
enabled: yes
state: restarted
Change "enabled" task containing service module.
- hosts: all
become: yes
tasks:
- name: Chenking ping
ping:
- name: Update packages
apt:
name: apache2
update_cache: yes
state: present
- name: restart apache2 server
service:
name: apache2
enabled: yes
state: restarted
- name: install php module
apt:
name: "{{ item }}"
state: present
with_items:
- php
- libapache2-mod-php5
- php-mcrypt
- php-mysql
I think last task is not required.

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 ---

How can I apply a tag to every command in an Ansible tasks file?

The Ansible best-practices documentation has this example code:
---
# file: roles/common/tasks/main.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
tags: ntp
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
tags: ntp
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
I'm looking to avoid duplicating the tags: ntp line. Is it possible for each of these instructions to inherit a tag?
You could work with - block:
➜ ~ cat become.yml
---
- hosts: localhost
user: vagrant
tasks:
- block:
- shell: whoami
register: result
- debug: var=result.stdout
- name: become_root_user
become: true
become_user: root
shell: whoami
register: sudo_test_result
- debug: var=sudo_test_result.stdout
tags:
- block1
- block:
- name: creating_new_app_user
become: true
become_user: root
become_method: sudo
user: name=app_user password=Bzs310di86b6E groups="adm,sudo" system=yes state=present
- name: become_app_user
become: true
become_user: app_user
become_method: sudo
shell: whoami
register: app_user_test_result
- debug: var=app_user_test_result.stdout
tags:
- block2
~ ansible-playbook -i realtime-automation/hosts-slaves become.yml --tags "block1"
In your specific case:
---
- block:
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
Before v2 this could be achieved assigning a tag to an 'include'
Move this task to a different file, say ntp.yml
---
# file: roles/common/tasks/ntp.yml
- name: be sure ntp is installed
yum: name=ntp state=installed
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
And then include it in main.yml
---
# file: roles/common/tasks/main.yml
- include: ntp.yml
tags: ntp

Resources