2
votes

Below is my playbook directory structure.

/home/ansible/playbooks/
├── first.yml --> simple playbook with no roles works fine.
├── playbook.yml  --> main playbook with roles
└── roles
    └── webservers
        ├── default
        │   └── main.yml
        ├── handler
        │   └── main.yml
        ├── tasks
        │   └── main.yml
        └── vars
            └── main.yml

My main playbook code below :

--- #playbook with roles

- hosts: webserver
  user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  roles:
    - webservers

My /roles/webserver/tasks/main.yml code :

- name: Install httpd on redhat
  yum:
    - pkg: httpd
      state: latest
  notify: installed_httpd
  when: ansible_os_family == 'RedHat'

- name: Install apache2 on debian
  apt:
    - pkg: apache2
      state: latest
  notify: installed_apache
  when: ansible_os_family == 'Debian'

My /roles/webserver/handlers/main.yml code :

- name: installed_httpd
  service:
  - name: httpd
    state: restarted

- name: installed_apache
  service:
  - name: apache2
    state: restarted

When I execute playbook, getting the following error :

ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to have been in '/home/ansible/playbooks/roles/webservers/tasks/main.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Install httpd on redhat
  ^ here

I checked all the questions related to this error but all seems to related improper syntax or incorrect indentation.

I have checked same in my files and all seems fine and also worked without error if I run playbook without roles with all tasks and handlers defined in the same playbook.

Ansible version: 2.7.2 python2 version: 2.7.5 python3 version: 3.7.1

1
All of these YAML files contain valid YAML, but the data loaded from them of course might not be what Ansible expects (that is, unless you have TAB characters in your files). I do recommend though to be more consistent in your formatting of YAML, to be able to spot any errors more easily. You now have different indents for sequences ( 2 positions before the elements, with no indent for hash as well as 4 positions with an offset of 2 for the hash: choose one and stick to it).Anthon

1 Answers

4
votes

There are syntax errors in declarations of the modules' attributes. Instead of

- name: Install httpd on redhat
  yum:
    - pkg: httpd

The correct syntax is

- name: Install httpd on redhat
  yum:
    pkg: httpd