Task need to be skipped by default by passing extra vars

52 views
Skip to first unread message

Kunalsing Thakur

unread,
Sep 27, 2018, 9:50:23 AM9/27/18
to Ansible Project
##### 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

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

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

```

Brian Coca

unread,
Sep 27, 2018, 10:01:02 AM9/27/18
to Ansible Project
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(','),[]) }}"

--
----------
Brian Coca

Kunalsing Thakur

unread,
Sep 27, 2018, 10:11:58 AM9/27/18
to Ansible Project
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(','),[]) }}"

Kunalsing Thakur

unread,
Sep 27, 2018, 10:13:41 AM9/27/18
to Ansible Project
Brian Can you just edit the playbook and show me how is going to work?

Kunalsing Thakur

unread,
Sep 27, 2018, 10:19:19 AM9/27/18
to Ansible Project
where i can define servicename in last condition


On Thursday, September 27, 2018 at 7:31:02 PM UTC+5:30, Brian Coca wrote:

Kunalsing Thakur

unread,
Sep 27, 2018, 12:52:25 PM9/27/18
to Ansible Project
Can you help me

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f91c31a2-d791-4206-9414-766d7aede3a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Sep 27, 2018, 1:01:08 PM9/27/18
to Ansible Project
just substitute for notdefined



--
----------
Brian Coca

Kunalsing Thakur

unread,
Sep 27, 2018, 1:16:55 PM9/27/18
to Ansible Project
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.

Kunalsing Thakur

unread,
Sep 27, 2018, 1:27:15 PM9/27/18
to Ansible Project
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.

Kunalsing Thakur

unread,
Sep 28, 2018, 12:46:37 AM9/28/18
to Ansible Project
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.

Kunalsing Thakur

unread,
Sep 28, 2018, 12:46:50 AM9/28/18
to Ansible Project

Kunalsing Thakur

unread,
Sep 28, 2018, 10:55:17 AM9/28/18
to Ansible Project
Brian Can you help me?
what can add in with_items?

Kunalsing Thakur

unread,
Oct 1, 2018, 3:22:23 AM10/1/18
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages