keepmoving
keepmoving

Reputation: 2043

How to exeucte role in Ansible

Today I am trying to write one ansible playbook for Jenkin and for that using role based approach. But I am facing some issue here, following are syntax that I am using to create Jenkins ansible.

Approch1 - site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
      include_role:
        name: jenkins

It gives me following error.

ERROR! 'include_role' is not a valid attribute for a Play

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Install Jenkins
  ^ here

Approch 2 - site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
     - include_role: 
        name: jenkins

This approach gives following error.

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 7, column 20, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  become: true
     - include_role:
                   ^ here

Approch 3 : site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
  tasks: 
     - include_role: 

it gives the following error:

ERROR! no action detected in task

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 8, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
     - include_role:
       ^ here
        name: jenkins

Approch 4 - site.yml:

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
  tasks: 
      include_role: 
      name: jenkins

It gives me following error.

ERROR! A malformed block was encountered.

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Install Jenkins
  ^ here

Could someone help on this what is right way to include role in playbook.

Thanks in Advance.

Upvotes: 0

Views: 928

Answers (1)

clockworknet
clockworknet

Reputation: 3056

I would suggest spending some more time with the docs, particularly the YAML docs. If you haven't worked with YAM/Ansible before, it can take a while for it to click and until it does, it can be hard to see why a space, - or : is needed at a given point.

A playbook is simply a 'list' of plays, so in YAML you start a list with:

---

- 

Each play is then composed of a 'dictionary' of parameters:

---

- hosts: localhost
  gather_facts: no     # Make sure you have at least one play early on that does gather facts
  connection: local    # For localhost only. This stops Ansible opening an SSH connection to its own host
  become: yes          # If you don't specify 'become_user:' defaults to root

There are more params, but they are the common ones. The final thing you need to do, is to tell the play what to do, and there are two parameters you can use. Both take a 'list' as the value. The first is a list of individual tasks:

 tasks:
   - name: Your first task
     some_ansible_module:
       module_param_1: some_value
       module_param_2: some_value
   - name: Your second task
     some__other_ansible_module:
       module_param_1: some_value
       module_param_2: some_value

The second is a list of roles to include:

 - roles:
     - role_name_1
     - role_name_2
     - role_name_2

There are more params you can pass when you are including roles, but that will get you started.

There are no reasons why you cannot use both tasks and roles in the same play, but in general it is best practice to use roles as much as possible.

So for your specific question, you need:

---

- hosts: localhost
  connection: local
  gather_facts: false
  become: true
  roles:
    - jenkins

Upvotes: 2

Related Questions