ansible hosts 格式
name1 ansible_ssh_host=*.*.*.* ansible_ssh_user=root ansible_ssh_pass=* ansible_ssh_port=22
name2 ansible_ssh_host=*.*.*.* ansible_ssh_user=root ansible_ssh_pass=* ansible_ssh_port=22
- name1 :别名,可以写服务器名称
- ansible_ssh_user:登陆账号,默认值在ansible.cfg中定义
- ansible_ssh_pass:登陆密码
- ansible_ssh_port:登录端口
加、解密 hosts
[root@yx01 ansible_playbook]# ansible-vault encrypt /etc/ansible/hosts
New Vault password:
Confirm New Vault password:
Encryption successful
ansible-vault decrypt /etc/ansible/hosts
playbook code
---
- hosts: all
remote_user: root
gather_facts: True
tasks:
- name: 休眠10秒,防止刚开始登陆负载飙升
shell: "sleep 10"
- name: 过滤负载信息
shell: "top -c -n 1 -b|grep load|grep -v grep|awk '{print $(NF-2),$(NF-1),$NF}'"
- name: 过滤cpu信息
shell: "top -c -n 1 -b|grep Cpu|grep -v grep|cut -c 10-"
shell code
#!/bin/bash
localtime=`date "+%Y-%m-%d_%H:%M:%S"`
logfile=logs/${
localtime}.txt
if [ "$(ls -A logs)" ]
then
echo "-------start time:$localtime-------"
rm -rf logs/*
fi
# 获取信息,--verbose将执行回显输出
# 添加 --ask-vault-pass参数手动交互式的输入密码
# 添加 --vault-password-file 指定存放密码的文件
ansible-playbook collect.yml --verbose > logs/tempfile.txt
# 过滤负载信息
cat logs/tempfile.txt |grep load|awk '{print $(NF-2),$(NF-1),$NF}'|sed 's/\[\"//g'|sed 's/\"\]\}//g' > $logfile
# 过滤cpu信息
cat logs/tempfile.txt|grep Cpu|egrep -o "\[\".*\"\]\}"|sed 's/\[\"//g'|sed 's/\"\]\}//g' >> $logfile
localtime=`date "+%Y-%m-%d_%H:%M:%S"`
echo "-------end time:$localtime-------"
解决ansible首次连接host服务器需验证问题
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
修改/etc/ansible/ansible.cfg,放开注释
输出结果
9.31, 6.05, 6.05
22.5 us, 5.6 sy, 0.0 ni, 70.4 id, 0.0 wa, 0.0 hi, 1.4 si, 0.0 st