Home > OS >  Creating a ansible playbook for getting the apache status and want to display just the status
Creating a ansible playbook for getting the apache status and want to display just the status

Time:01-01

I have written an Ansible playbook which is as follows, but the problem it shows the status as disabled when i launch the playbook and when i check the same on my ubuntu server manually for the server status it shows active.

Please help and suggest what mistake I am doing in writing the playbook .

Note :- My goal is to get the status of the apache server {that is it in started or stopped status} and either print on the screen or append it in some abc.txt file and it should works every-time and update the abc.txt file with the status

testfile.yml

---
  -
    name: "To check if the apache is running or not"
    hosts: webserver1
    become: yes
    tasks:

      - name: Check Apache2 service status
        service:
          name: apache2
          state: started
        register: apache2_status

    # - name: apache2 status
    #   command: service apache2 status
    #   register: apache2_status   
      
      - name: Print Apache2 service status
        debug:
          msg: "Apache2 service is {{ 'started' if apache2_status.status == 'started' else 'stopped' }}"

Running the following ansible command to run the playbook

ansible-playbook testfile.yml -i inventory --ask-become-pass

output

PLAY [To check if the apache is running or not] ********************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [webserver1]

TASK [Check Apache2 service status] ********************************************************************************************
ok: [webserver1]

TASK [Print Apache2 service status] ********************************************************************************************
ok: [webserver1] => {
    "msg": "Apache2 service is stopped"
}

PLAY RECAP *********************************************************************************************************************
webserver1                 : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

And When I check the same manually on my ubuntu webserver1

ubuntu@ubuntu1:~$ service apache2 status
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-12-31 08:56:30 UTC; 5h 28min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 632 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 708 (apache2)
      Tasks: 55 (limit: 1695)
     Memory: 7.5M
        CPU: 2.724s
     CGroup: /system.slice/apache2.service
             ├─708 /usr/sbin/apache2 -k start
             ├─713 /usr/sbin/apache2 -k start
             └─714 /usr/sbin/apache2 -k start

Dec 31 08:56:29 ubuntu1 systemd[1]: Starting The Apache HTTP Server...
Dec 31 08:56:30 ubuntu1 apachectl[685]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name,>
Dec 31 08:56:30 ubuntu1 systemd[1]: Started The Apache HTTP Server.

I have tried to run the Ansible playbook with different parameters but not able to replicate my expected output

What my expecting is My goal is to get the status of the apache server {that is it in started or stopped status ?} and either print on the screen or append it in some abc.txt file and it should works every-time and update the abc.txt file with the new status

CodePudding user response:

For example, given the inventory

shell> cat hosts
[webservers]
webserver1 ansible_host=10.1.0.74

[webservers:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo
ansible_python_interpreter=/bin/python3.6

The playbook below in the first task makes sure the web server is running. The second task collects the status of all services. The third task writes the status to a file at the controller

- name: Get status of webserver and append the new status to file
  hosts: webservers

  tasks:

    - name: Start lighttpd if not running already
      service:
        name: lighttpd
        state: started
      register: status
    - debug:
        var: status.state

    - name:
      service_facts:
    - debug:
        var: ansible_facts.services['lighttpd.service']

    - lineinfile:
        create: true
        path: /tmp/webservers.status
        line: "{{ '%Y-%m-%d %H:%M:%S'|strftime }} {{ item }} {{ s.state }} {{ s.status }}"
      loop: "{{ ansible_play_hosts }}"
      run_once: true
      delegate_to: localhost
      vars:
        s: "{{ hostvars[item].ansible_facts.services['lighttpd.service'] }}"

gives

PLAY [Get status of webserver and append the new status to file] *****************************

TASK [Start lighttpd if not running already] *************************************************
ok: [webserver1]

TASK [debug] *********************************************************************************
ok: [webserver1] => 
  status.state: started

TASK [service_facts] *************************************************************************
ok: [webserver1]

TASK [debug] *********************************************************************************
ok: [webserver1] => 
  ansible_facts.services['lighttpd.service']:
    name: lighttpd.service
    source: systemd
    state: running
    status: disabled

TASK [lineinfile] ****************************************************************************
changed: [webserver1 -> localhost] => (item=webserver1)

PLAY RECAP ***********************************************************************************
webserver1: ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

and appends the status of the webserver(s) to the file

shell> cat /tmp/webservers.status 
2023-01-01 01:03:15 webserver1 running disabled
2023-01-01 01:03:32 webserver1 running disabled

CodePudding user response:

I'm pretty sure your problem is that you want to check the state, but you are effectively checking whether the service is enabled or not.

(ie a service can be currently running but not enabled, and even though your service is enabled, it is not specified in ansible, and it is not collecting that for you)

I'd suggest using systemd instead of service like this. Set the status to started, but in check mode. The module won't actually do anything, but it will set "changed" to true if a change needed to be made.

This is more Ansible-ish in my opinion than calling a shell command.

(I tested this on Fedora -- adjust the service name as you need to.)


---
- hosts: localhost

  tasks:
  - name: check if apache is running
    ansible.builtin.systemd:
      # on Ubuntu, I think this is apache2
      name: httpd
      state: started
    register: httpd_status
    check_mode: true

  - name: debug
    ansible.builtin.debug:
      msg: "Apache2 service is {{ httpd_status.changed | ternary('stopped','started') }}"

  • Related