ansible自动化运维工具的安装,非免密使用,免密使用

安装ansible

比较简单,管理主机直接yum -y install ansible

如果管理机跟主机之间没有配置免密登录,需要如下配置:

管理主机:192.168.46.134

分组主机:192.168.46.125

192.468.46.126

192.168.46.127

1:修改ansible配置文件/etc/ansible/ansible.cfg

将#host_key_checking = False

修改成host_key_checking = False //将注释去掉

2:修改host文件,或者自己定义文件也行,目录不限制,添加以下内容到文件。

注意主机ip,如果不想用IP地址可以修改/etc/hosts文件进行域名解析,因为管理机到各主机非免密,所以需要将,密码写到文件里面。分组主机的密码必须一致。

[a]
192.168.46.125
192.168.46.126
192.168.46.127

[a:vars]
ansible_ssh_user="root"
ansible_ssh_pass="23we@#WE"

测试:本次没有配置域名解析,警告不用管。

[root@LNMP ansible]# ansible -i hosts a   -m shell -a "ls /"
[WARNING]:  * Failed to parse /etc/ansible/hosts with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: No JSON object could be decoded  Syntax Error while loading
YAML.   did not find expected <document start>  The error appears to be in '/etc/ansible/hosts': line 45, column 1, but may be elsewhere in the file depending on the exact syntax problem.  The offending line appears to be:
[a] 192.168.46.125 ^ here
[WARNING]:  * Failed to parse /etc/ansible/hosts with ini plugin: /etc/ansible/hosts:52: Expected key=value, got: r.
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
192.168.46.126 | CHANGED | rc=0 >>
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
192.168.46.125 | CHANGED | rc=0 >>
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
192.168.46.127 | CHANGED | rc=0 >>
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

如果管理机跟主机之间有免密登录:

1:批量做免密,本次用脚本执行,插入以下脚本:


#!/bin/bash

USER=root
PASS=23we@#WE

rm -f /root/.ssh/id_rsa
(ssh-keygen -P "" -f /root/.ssh/id_rsa) &>/dev/null
(yum -y install sshpass) &>/dev/null

## 在'{ }'中定义范围 如下举例 4为起始 250为结束 
for i in {125..127}
do
## 在IP_host变量中定义网段
    IP_host=192.168.46.${i}
    ping  -c 1 -w 1 ${IP_host} &>/dev/null
    if [ $? -eq 0 ];then
        (sshpass -p ${PASS} ssh-copy-id -o StrictHostKeyChecking=no ${IP_host}) &>/dev/null
        echo -e "\e[1;32m ${IP_host}    ok \e[0m"
    fi
done

 ip地址范围,用户名密码需要修改

 测试免密:

[root@LNMP tmp]# ssh 192.168.46.125
Last login: Thu May 26 02:48:56 2022 from 192.168.46.134
[root@bogon ~]# ^C
[root@bogon ~]# logout
Connection to 192.168.46.125 closed.
[root@LNMP tmp]# ssh 192.168.46.126
Last login: Thu May 26 02:48:56 2022 from 192.168.46.134

测试ansible:

1:在管理主机新建主机文件并添加以下内容
[


[host]
192.168.46.125
192.168.46.126
192.168.46.127

2:测试ansible可用性

[root@ansible tmp]# ansible -i hosts host -m shell -a "ls -l /tmp/"
192.168.46.127 | CHANGED | rc=0 >>
total 4
drwx------. 2 root root  41 May 26 03:32 ansible_command_payload__G4799
drwx------. 2 root root  41 May 26 03:34 ansible_command_payload_Yd9OIU
-rwx------. 1 root root 836 May 26  2022 ks-script-jUhcqr
drwx------. 3 root root  17 May 26  2022 systemd-private-ffd44fde0d694597b97c7554de1cba9f-chronyd.service-yYffBA
drwx------. 2 root root   6 May 26  2022 vmware-root_678-2722697728
-rw-------. 1 root root   0 May 26  2022 yum.log
192.168.46.126 | CHANGED | rc=0 >>
total 8
drwx------. 2 root root  41 May 26 03:34 ansible_command_payload_IK9U1P
drwx------. 2 root root  41 May 26 03:32 ansible_command_payload_ys1Tcc
-rw-r--r--. 1 root root  52 May 26 03:26 hosts
-rwx------. 1 root root 836 May 26  2022 ks-script-M8Bsk4
drwx------. 3 root root  17 May 26  2022 systemd-private-284c9c4b88ad4d8b91b123c5702abdd1-chronyd.service-pHAJkU
drwx------. 2 root root   6 May 26  2022 vmware-root_674-2731152261
-rw-------. 1 root root   0 May 26  2022 yum.log
192.168.46.125 | CHANGED | rc=0 >>
total 4
drwx------. 2 root root  41 May 26 03:34 ansible_command_payload_DoTPcv
drwx------. 2 root root  41 May 26 03:32 ansible_command_payload_vW1Ww0
-rwx------. 1 root root 836 May 26  2022 ks-script-fuRxvj
drwx------. 3 root root  17 May 26  2022 systemd-private-5edf5ee89f2842c489587a2e14600adf-chronyd.service-cVopir
drwx------. 2 root root   6 May 26  2022 vmware-root_676-2731021186
-rw-------. 1 root root   0 May 26  2022 yum.log
[root@ansible tmp]# vim hosts

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值