# Task need to be skipped by default by passing extra vars

**URL:** <https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662>\
**Category:** Ansible Project\
**Created:** [September 27, 2018, 1:50pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662 "2018-09-27T13:50:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 1:50pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/1 "2018-09-27T13:50:22Z")

</div>

##### SUMMARY

When i want to use specific tag in playbook from role it will run all task which is defined in role.

##### ISSUE TYPE

- Bug Report

##### COMPONENT NAME

ansible-core

##### ANSIBLE VERSION

```auto
ansible 2.6.1
config file = /etc/ansible/playbooks/example/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

```

##### CONFIGURATION

```auto
ansible 2.6.1
config file = /etc/ansible/playbooks/example/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

```

##### OS / ENVIRONMENT

CentOS Linux release 7.5.1804 (Core)

##### STEPS TO REPRODUCE

```yaml
roles/common/tasks/main.yml
---
- name: Reloading Service on {{ansible_hostname}}
service:
name: "{{ item }}"
state: reloaded
with_items: {{ servicename.split(',') | default([]) }}

- name: checking the service status {{ansible_hostname}}
service:
name: "{{ item }}"
state: started
with_items: {{ servicename.split(',') | default([]) }}

```

```YAML
playbook.yaml
- name: checking service is skipped by default
hosts: {{host}}
vars_files:
- /etc/ansible/inventories/group_vars/common.yaml
serial: 1
roles:
- common

```

#####ansible command  
ansible-playbook -vv --vault-id sre@/opt/sre -i /etc/ansible/inventories/funccphosts test.yml --extra-vars “host=node1,node2”

##### EXPECTED RESULTS

i wants to just skip the task of service if i am not passing any servicename variable

##### ACTUAL RESULTS

```auto
TASK [common_role_ : Reloading Service on node1] ****************************
task path: /etc/ansible/roles/common_role_cow/tasks/main.yml:17
Thursday 27 September 2018 13:06:27 +0000 (0:00:00.529) 0:00:09.391 ****
fatal: [node1]: FAILED! =>
msg: '''list object'' has no attribute ''split'''

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 27, 2018, 2:00pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/2 "2018-09-27T14:00:20Z")

</div>

You are not using |default correctly, the split needs a defined  
variable to work on:

with\_items: {{ (servicename|default('')).split(',') }}

That still creates a list with single blank item so you might want to  
add a defined check

with\_items: "{{ (notdefined is  
defined)|ternary((notdefined|default('')).split(','),) }}"

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 2:11pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/3 "2018-09-27T14:11:58Z")

</div>

So r u saying like this.

- name: Reloading Service on {{ansible\_fqdn}}  
service:  
name: “{{ item }}”  
state: reloaded  
with\_items: “{{ (notdefined is defined)|ternary((notdefined|default(‘’)).split(‘,’),) }}”  
register: result

- name: checking the service status on {{ansible\_fqdn}}  
service:  
name: “{{ item }}”  
state: started  
with\_items: “{{ (notdefined is defined)|ternary((notdefined|default(‘’)).split(‘,’),) }}”

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 2:13pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/4 "2018-09-27T14:13:40Z")

</div>

Brian Can you just edit the playbook and show me how is going to work?

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 2:19pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/5 "2018-09-27T14:19:19Z")

</div>

where i can define servicename in last condition

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 4:52pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/6 "2018-09-27T16:52:03Z")

</div>

Can you help me

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 27, 2018, 5:00pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/7 "2018-09-27T17:00:29Z")

</div>

just substitute for notdefined

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 5:16pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/8 "2018-09-27T17:16:50Z")

</div>

Like this.

- name: Reloading Service on {{ansible\_fqdn}}  
service:  
name: “{{ item }}”  
state: reloaded

with\_items: “{{ (notdefined is defined)|ternary((notdefined|default(‘’)).split(‘,’),) }}”

But i don’t see servicename there.

Can you just write one line of with\_items so that i can understand what you are saying.

I want like this  
ansible-playbook -vv --vault-id sre@/opt/sre -i /etc/ansible/inventories/funccphosts test.yml --extra-vars “host=node1,node2”  
If i pass service name like this  
ansible-playbook -vv --vault-id sre@/opt/sre -i /etc/ansible/inventories/funccphosts test.yml --extra-vars “host=node1,node2 servicename=httpd”  
then it will work.

Your help will be much appreciated looking for your reply.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 27, 2018, 5:27pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/9 "2018-09-27T17:27:15Z")

</div>

if i did not pass servicename while running ansible-playbook it will skipp the service task  
and if i pass servicename the service will reloaded and check whether service is running or not.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 28, 2018, 4:46am UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/10 "2018-09-28T04:46:37Z")

</div>

Hi Brian,

i have defined like this:-

- name: Reloading Service on {{ansible\_fqdn}}  
service:  
name: “{{ item }}”  
state: reloaded  
with\_items: “{{ servicename.split(‘,’) | ternary((notdefined|default(‘’)).split(‘,’),) }}”

fatal: [node1]: FAILED! =\>  
msg: ‘’‘list object’’ has no attribute ‘‘split’’’

Again getting error.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 28, 2018, 4:46am UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/11 "2018-09-28T04:46:50Z")

</div>



---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [September 28, 2018, 2:55pm UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/12 "2018-09-28T14:55:17Z")

</div>

Brian Can you help me?  
what can add in with\_items?

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [October 1, 2018, 7:22am UTC](https://forum.ansible.com/t/task-need-to-be-skipped-by-default-by-passing-extra-vars/28662/13 "2018-10-01T07:22:23Z")

</div>

Hi Brian,

As you suggested its worked for input parameter but not for default it giving failed task

- name: Reloading Service  
service:  
name: “{{ item }}”  
state: reloaded  
with\_items: “{{ servicename.split(‘,’) | ternary((servicename|default(‘’)).split(‘,’),) }}”

Output result.  
ansible-playbook --extra-vars “host=node1 servicename=” test.yml

TASK [role\_common : Reloading Service] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/tasks/common.yml:17  
Monday 01 October 2018 07:19:09 +0000 (0:00:00.582) 0:00:12.273 \*\*\*\*\*\*\*\*  
failed: [node1] (item=) =\> changed=false  
item: ‘’  
msg: ‘Could not find the requested service : host’

Waiting for your response.
