Ansible World

79 views
Skip to first unread message

madan gopal

unread,
Sep 17, 2020, 6:17:29 AM9/17/20
to Ansible Project
Hello All,

I am completely new to Ansible and wanted to start my first baby steps with simple playbook which will help me out in my day-to-day works

Problem Statement: To verify remote server uptime both Windows and Linux
Solution: Prompt for ServerName and display uptime of the server

Linux : uptime
Windows: systeminfo | find “System Boot Time:”

This is my playbook which is not working,

---
- name: Get server uptime
   hosts: all
   vars_prompt:
    - name: "Server Name"
      prompt: "Enter VSphere Hostname or IP Address"
      private: no

    - name: "Username"
      prompt: "Enter Server Login Username"
      private: no
     
    - name: "Password"
      prompt: "Enter Server Login Password"
     
   - name: "VMname"
     prompt: "Enter VM Hostname"
     private: no

tasks:
 - name: Get Server uptime first
   shell: uptime
   register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"

 - name: Get System date and time
    shell: date
    register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"

I tried different ways but, cannot call different OS versions in one playbook.

Any sort of assistance is greatly appreciated.

Note: It should review the Operating System of the Server and accordingly run "uptime" command to display the output.

Regards,
Madan Gopal

madan gopal

unread,
Sep 18, 2020, 1:59:37 PM9/18/20
to Ansible Project
Helo all,

Can anyone help me with this playbook ? Any path to achieve my end goal also helps

Regards,
Madan

J C

unread,
Sep 18, 2020, 7:24:29 PM9/18/20
to ansible...@googlegroups.com
I am assuming you are using all of the defaults.  So what does your host file look like?    I normally use ansible on the networking side - so it is little different then servers so take everything i am about to say with a grain of salt.  I don't have a server to test these commands against - this is just how I would start to approach it.  

I would think that you would want use gather facts to find out what server type you are hitting or assign the variable in your host file.   Your vars prompt is not needed, you should have that information in your host file.  When you run your playbook it will use the username that you are logged in as on the control node.  If you want to change that you would use the -u in the command when you execut the playbook.   Also the password you would use a -k in the command.

Also it looks like the shell command module will not work with windows.  ( https://docs.ansible.com/ansible/latest/modules/shell_module.html ) --- It looks like there is a winshell module too though  https://docs.ansible.com/ansible/latest/modules/win_shell_module.html#win-shell-module --- 

So you will need to add a when statement in your tasks to only execute when it is a linux or windows server and create a new task for windows.

You should check out https://docs.ansible.com/ansible/latest/modules/modules_by_category.html to see what all of the modules do.  Ansible has pretty good documentation.  Hope this helps and doesn't muddy the waters too much.


so the host file would be something like this

[Test_Servers]


your playbook would be something like that

---
name: Uptime Play
hosts: Test_Servers

tasks:
 - name: Get Server uptime first
     shell: uptime
     register: hello
   when: ansible_system is "Linux"

- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"

 - name: Get System date and time
    shell: date
    register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/879f4c54-b522-4c95-b458-37a16f30ee7fn%40googlegroups.com.

madan gopal

unread,
Sep 18, 2020, 11:36:31 PM9/18/20
to Ansible Project
Hello all,

Thank you very much for your inputs. I have modified my playbook as below

---
- name: Get server uptime
  hosts: all
  gather_facts: yes
  vars_prompt:
   - name: "Server Name"
     prompt: "Enter VM Hostname or IP Address"
     private: no

   - name: "Username"
     prompt: "Enter VM Login Username"
     private: no

   - name: "Password"
     prompt: "Enter VM Login Password"

  tasks:
    - block:
       - name: Get Server uptime
         shell: "uptime"
         register: uptime

       - debug:
           var: uptime

       - name: Get System date and time
         shell: "date"
         register: date

       - debug:
           var: date
      when: ansible_os_family == "Linux"

    - block:
       - name: Get Server uptime
         win_command: systeminfo | find "Boot Time"
         register: uptime

       - debug:
           var: uptime
      when: ansible_os_family == "Windows"

Host file is already like you said, my host file looks like this

[Linux]
RHELTest01

[Windows]
WinTest01


Error i am getting,

[test@ansible ~]$ ansible-playbook ServerUptime.yml
Enter VM Hostname or IP Address: test
Enter VM Login Username: test
Enter VM Login Password:

PLAY [Get server uptime] *******************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************
fatal: [RHELTest01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
fatal: [WinTest01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Nutanix Controller VM\nPermission denied (publickey,password).", "unreachable": true}

PLAY RECAP *********************************************************************************************************************************************************
WinTest01             : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
RHELTest01           : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0



Still my requirement is not fulfilled. It is trying to connect with SSH for Windows machines also.I am looking for a playbook which will take user input and validate the OS to display uptime of the server(VM). Any sort of help is greatly appreciated.

Reggards,
Madan
Reply all
Reply to author
Forward
0 new messages