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