Centos7 ansible安装使用
来源:互联网 发布:源码交易网站源码 编辑:程序博客网 时间:2024/06/05 10:58
ansible安装使用
1、创建sudo账号(batch)
2、安装ansible(基于秘钥)
3、验证
ansible server:172.17.10.241
ansible node1:172.17.10.242
ansible node2:172.17.10.243
一、创建sudo账号(batch)
实现:shell批量登陆和执行创建用户脚本
一共五个文件(batch.sh ip sh.exp useradd.exp useradd.sh),将五个文件放在linux主机一个目录下
主脚本batch.sh(用于批量调用sh.exp useradd.exp useradd.sh无交互脚本)
==================================================
#!/bin/bash
while read line
do
ip=$line
username=root
userpasswd=1234asdf/
expect useradd.exp $ip $username $userpasswd
expect sh.exp $ip $username $userpasswd
done < ip
==================================================
ip文件就是要登录的远程主机IP
useradd.exp上传useradd.sh脚本到远程服务器/root目录下
==================================================
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set userpasswd [lindex $argv 2]
set timeout 10
spawn scp useradd.sh $username@$ip:/root/
expect {
"(yes/no)?" {send "yes\r";exp_continue}
"*password:" {send "$userpasswd\r"}
}
expect eof
==================================================
sh.exp执行useradd.sh脚本
==================================================
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set userpasswd [lindex $argv 2]
set timeout 3
spawn ssh $username@$ip
expect {
"yse/no" {send "yes\r";exp_continue}
"*password*" {send "$userpasswd\r";exp_continue}
}
expect "*]$"
send "sh /root/useradd.sh\r"
send "exit\r"
expect eof
==================================================
useradd.sh添加用户并且给sudo权限
==================================================
#!/bin/bash
name=batch
grep -w $name /etc/passwd &>/dev/null
if [ $? == 0 ];then
echo "$name is exit!"
else
useradd -m $name
echo "1234asdf/" | passwd --stdin $name
echo "$name is create success!"
fi
echo "$name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
grep -w $name /etc/sudoers &>/dev/null
if [ $? != 0 ];then
echo "$name is exit!"
else
echo "$name sudo add success!"
fi
==================================================
2.1
ansible仓库默认不在yum仓库中,需要我们自己启用epel仓库
rpm -iUvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum安装ansible
yum -y install ansible
检查ansible版本
ansible --version
2.2
设置用于节点鉴权的SSH秘钥, 在ansible server端生成秘钥,并且复制公钥到节点中
ssh-keygen #生成秘钥
ssh-copy-id -i batch@172.17.10.242
ssh-copy-id -i batch@172.17.10.243 #复制公钥到节点中
ansible修改配置文件
ansible配置文件在/etc/ansible/ansible.cfg
grep -v "^#" ansible.cfg | grep -v "^$"
[defaults]
inventory = /etc/ansible/hosts
remote_tmp = ~/.ansible/tmp
local_tmp = ~/.ansible/tmp
forks = 5
poll_interval = 15
sudo_user = root
transport = smart
remote_port = 22
host_key_checking = False
deprecation_warnings = False
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
ansible主机定义
ansible主机定义在/etc/ansible/hosts配置文件中
vim /etc/ansible/hosts
[test]
172.17.10.242 ansible_user=batch
172.17.10.243 ansible_user=batch
1、创建sudo账号(batch)
2、安装ansible(基于秘钥)
3、验证
ansible server:172.17.10.241
ansible node1:172.17.10.242
ansible node2:172.17.10.243
一、创建sudo账号(batch)
实现:shell批量登陆和执行创建用户脚本
一共五个文件(batch.sh ip sh.exp useradd.exp useradd.sh),将五个文件放在linux主机一个目录下
主脚本batch.sh(用于批量调用sh.exp useradd.exp useradd.sh无交互脚本)
==================================================
#!/bin/bash
while read line
do
ip=$line
username=root
userpasswd=1234asdf/
expect useradd.exp $ip $username $userpasswd
expect sh.exp $ip $username $userpasswd
done < ip
==================================================
ip文件就是要登录的远程主机IP
useradd.exp上传useradd.sh脚本到远程服务器/root目录下
==================================================
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set userpasswd [lindex $argv 2]
set timeout 10
spawn scp useradd.sh $username@$ip:/root/
expect {
"(yes/no)?" {send "yes\r";exp_continue}
"*password:" {send "$userpasswd\r"}
}
expect eof
==================================================
sh.exp执行useradd.sh脚本
==================================================
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set userpasswd [lindex $argv 2]
set timeout 3
spawn ssh $username@$ip
expect {
"yse/no" {send "yes\r";exp_continue}
"*password*" {send "$userpasswd\r";exp_continue}
}
expect "*]$"
send "sh /root/useradd.sh\r"
send "exit\r"
expect eof
==================================================
useradd.sh添加用户并且给sudo权限
==================================================
#!/bin/bash
name=batch
grep -w $name /etc/passwd &>/dev/null
if [ $? == 0 ];then
echo "$name is exit!"
else
useradd -m $name
echo "1234asdf/" | passwd --stdin $name
echo "$name is create success!"
fi
echo "$name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
grep -w $name /etc/sudoers &>/dev/null
if [ $? != 0 ];then
echo "$name is exit!"
else
echo "$name sudo add success!"
fi
==================================================
效果图:
2.1
ansible仓库默认不在yum仓库中,需要我们自己启用epel仓库
rpm -iUvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum安装ansible
yum -y install ansible
检查ansible版本
ansible --version
2.2
设置用于节点鉴权的SSH秘钥, 在ansible server端生成秘钥,并且复制公钥到节点中
ssh-keygen #生成秘钥
ssh-copy-id -i batch@172.17.10.242
ssh-copy-id -i batch@172.17.10.243 #复制公钥到节点中
ansible修改配置文件
ansible配置文件在/etc/ansible/ansible.cfg
grep -v "^#" ansible.cfg | grep -v "^$"
[defaults]
inventory = /etc/ansible/hosts
remote_tmp = ~/.ansible/tmp
local_tmp = ~/.ansible/tmp
forks = 5
poll_interval = 15
sudo_user = root
transport = smart
remote_port = 22
host_key_checking = False
deprecation_warnings = False
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
如果未开启deprecation_warnings = False,效果如图
ansible主机定义
ansible主机定义在/etc/ansible/hosts配置文件中
vim /etc/ansible/hosts
[test]
172.17.10.242 ansible_user=batch
172.17.10.243 ansible_user=batch
ansible test -m ping
ansible配置文件详解:http://www.cnblogs.com/LuisYang/p/5960660.html
阅读全文
0 0
- Centos7 ansible安装使用
- CentOS7 安装 Ansible
- Centos7安装ansible
- CentOS7 安装Ansible - No package ansible available
- CentOS6或CentOS7安装ansible
- centos7上ansible初步使用
- 安装和使用ansible
- Ansible安装和使用
- ansible +centos7 批量安装zabbix agent
- centos7安装ansible遇到的问题
- Centos7 自动化工具 Ansible 安装配置
- centos7上使用ansible+murder分发文件
- Ansible 安装与简单使用
- Ansible的安装与使用
- ansible的安装与使用
- ansible使用playbook安装nginx
- 【Python】Ansible安装与使用
- 使用 Ansible 安装部署 TiDB
- Java类加载器加载过程
- python __getitem__()方法理解
- Andriod
- P1736 创意吃鱼法
- 反射机制理解之一
- Centos7 ansible安装使用
- UITextView禁用复制粘贴放大
- Hive整体架构
- 图像处理基础教程和工具软件简介
- VS中堆栈溢出问题
- pyjsonrpc+multiprocessing实现可并发处理RPC服务
- SSL2790 2017年10月25日提高组T2 次方的运算(math)
- 中缀转后缀
- TypeScript 的索引签名 的笔记