Ansible – Enable and Restart a service

In this case “syslog-ng” has been used.

- hosts: [targethosts]

  become: yes
  become_method: sudo

    - name: Gather Executing User Name
      command: whoami
      always_run: yes
      register: executing_user_id
    - name: Restart syslog-ng service
      service: name=syslog-ng  state=restarted
    - name: Enable service
      service: name=syslog-ng  enabled=yes

    - name: Start writing to Ansible Log file
      lineinfile: dest="/var/log/ansible_history" line="TI-4003 syslog-ng  -  DATE {{ ansible_date_time.iso8601 }} - USER {{ executing_user_id.stdout }}" create=yes state=present insertafter=EOF
Share Button