Task need to be skipped by default by passing extra vars

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
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
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
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([]) }}
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
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'''

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(','),) }}"

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(‘,’),) }}”

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

where i can define servicename in last condition

Can you help me

just substitute for notdefined

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.

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.

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.

Brian Can you help me?
what can add in with_items?

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.