YAML Script: Second ansible playbook to get date and server uptime status.

Lets start creating YAML scripts in ansible.

The first script we have created included the ping response of all hosts.

The second YAML(Yet Another Markup Language) script we have created is to get server uptime, and server date/time.

There are two tasks created to get these two outputs.

YAML Ansible Script to get server date and uptime

$ cat playbooks/firstplaybook.yml
---
- name: Get server uptime
hosts: all

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

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


Explanation
1. — marks the start and end of a YAML script.
2. – marks the member of the list.
3. Key: Value represents a dictionary in YML which defines something.
4. “name:” key defined with value “Get server uptime”
5. “hosts” key defines on what all hosts we want to run below task(s).
6. “all” is a default identifier which contains all hosts defined in the hosts file.
7. “tasks” defines the various tasks we want to perform in this YML script.

Tasks Definition
1. “- name:” –> Here “-” marks the member of the list task and name is the key with value “Get uptime first”
2. “shell:” –> Module defined in ansible to execute any command on the login shell of the user.
3. “register:” –> Ansible provides register key that contains a variable name. The variable holds the output of the present task. We can use these variable to get the output, and error thrown by our task.
4. “debug: “ list item is available under ansible register and used to print the output captured inside the variable.
5. stdout and stderr are two different keywords to print the output and error encountered during the task run.


Output

$ ansible-playbook playbooks/firstplaybook.yml

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

TASK [Gathering Facts] *************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

TASK [Get uptime first] ************************************************************************************************************
changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
"msg": " 06:06:23 up 21 days, 13:29, 1 user, load average: 0.72, 0.46, 0.37"
}
ok: [r168-host1.ngelinux.com] => {
"msg": " 06:06:21 up 21 days, 23:28, 1 user, load average: 0.41, 0.44, 0.40"
}

TASK [debug] ***********************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
"msg": ""
}
ok: [r168-host1.ngelinux.com] => {
"msg": ""
}

TASK [Get date second] *************************************************************************************************************
changed: [r168-host1.ngelinux.com]
changed: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
"msg": "Tue Dec 4 06:06:32 GMT 2018"
}
ok: [r168-host1.ngelinux.com] => {
"msg": "Tue Dec 4 06:06:30 GMT 2018"
}

TASK [debug] ***********************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
"msg": ""
}
ok: [r168-host1.ngelinux.com] => {
"msg": ""
}

PLAY RECAP *************************************************************************************************************************
r005-host1.ngelinux.com : ok=7 changed=2 unreachable=0 failed=0
r168-host1.ngelinux.com : ok=7 changed=2 unreachable=0 failed=0

$

0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback
1 year ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]

trackback
1 year ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]

trackback
1 year ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]

trackback
2 years ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]

trackback
2 years ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]

trackback
2 years ago

[…] YAML Script: Second ansible playbook to get date and server uptime status. […]