본문 바로가기

Automation Tools/Ansible

8.2 ansible | [playbook] Linux service 상태 변경

반응형

---
- name: "Change Service State "
  hosts: all
  become: true
  tasks:
    - name: "Change Service State "
      service:
        name: "{{ service_name }}"
        # state : reloaded|restarted|started|stopped
        state: "{{ service_state }}"
        # enabled : yes|no
        enabled: "{{ service_enable }}"
      # 조건 : centos 6 or 7 인 경우만
      when:
        - ansible_facts['distribution'] == "CentOS"
        - ( ansible_facts['distribution_major_version'] == "6" or ansible_facts['distribution_major_version'] == "7" )
    - name: "Change Service State "
      service:
        name: "{{ service_name }}"
        # 서비스 바이너리 경로 ex)/usr/bin/name
        pattern: "{{ service_path }}"
        state: "{{ service_state }}"
        enabled: "{{ service_enable }}"
      when:
        - ansible_facts['distribution'] != "CentOS"

 

반응형