Ansible:执行命令输出,进入另一个命令
Ansible: execute command output, into another command
我打算创建并 运行 一个 ansible 剧本(或 .sh 脚本)来检测上次用户登录,然后如果登录日期早于 30 天,则继续关闭服务器,否则停止剧本。
我对ansible的了解是基础的,脚本也是。 :(
我将在我的 RHEL 服务器 6.x 和 7.x
上执行此操作
为测试用户执行最后一条命令
last | grep test
test pts/1 127.0.0.1 Mon Sep 9 10:53 - 10:53 (00:00)
保存此输出,如果日期早于 30 天,则关闭服务器。我如何将此输出设置为下一个命令的变量?
if [ "$(date +%D)" != "30" ];then
shutdown -h now
fi
你应该尝试自己想出这个,但你可以从这里开始:
- name: Check user logged in (30 days)
shell: last | grep test
register: user_last_login
ignore_errors: True
- name: Shut down server if no user login
shell: shutdown -h now
become: yes
when: user_last_login
我不会告诉你最后一点(如何检查 user_last_login 是否超过 30 天)。
提示(Ansible中有处理时间的函数(基于Python):
( '10 Apr 19 12:52 UTC'|to_datetime("%d %b %y %H:%M %Z") - "1970 UTC"|to_datetime("%Y %Z")).total_seconds()
This is how i solved the problem
---
- name: Check last login
hosts: REDHAT
tasks:
- name: Last logged user
shell: last | grep myusername
register: output
ignore_errors: yes
- name: shutdown server
shell: shutdown -h now
become: yes
when: (output.stdout|int - ansible_date_time.epoch|int) < 2592000
我打算创建并 运行 一个 ansible 剧本(或 .sh 脚本)来检测上次用户登录,然后如果登录日期早于 30 天,则继续关闭服务器,否则停止剧本。
我对ansible的了解是基础的,脚本也是。 :(
我将在我的 RHEL 服务器 6.x 和 7.x
上执行此操作为测试用户执行最后一条命令
last | grep test
test pts/1 127.0.0.1 Mon Sep 9 10:53 - 10:53 (00:00)
保存此输出,如果日期早于 30 天,则关闭服务器。我如何将此输出设置为下一个命令的变量?
if [ "$(date +%D)" != "30" ];then
shutdown -h now
fi
你应该尝试自己想出这个,但你可以从这里开始:
- name: Check user logged in (30 days)
shell: last | grep test
register: user_last_login
ignore_errors: True
- name: Shut down server if no user login
shell: shutdown -h now
become: yes
when: user_last_login
我不会告诉你最后一点(如何检查 user_last_login 是否超过 30 天)。
提示(Ansible中有处理时间的函数(基于Python):
( '10 Apr 19 12:52 UTC'|to_datetime("%d %b %y %H:%M %Z") - "1970 UTC"|to_datetime("%Y %Z")).total_seconds()
This is how i solved the problem
---
- name: Check last login
hosts: REDHAT
tasks:
- name: Last logged user
shell: last | grep myusername
register: output
ignore_errors: yes
- name: shutdown server
shell: shutdown -h now
become: yes
when: (output.stdout|int - ansible_date_time.epoch|int) < 2592000