暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

自动化工具ansible服务部署与使用

浙金科技无限 2020-07-10
303

使用ansible需要首先实现ssh密钥连接
ssh+key实现基于密钥连接(ansible使用前提)
ansible其功能实现基于SSH远程连接服务


●不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端
●需要依靠大量的模块实现批量管理
●配置文件

/etc/ansible/ansible.cfg (前期不用配置)


01

配置互信秘钥对


[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):  

#私钥创建后保存的路径

Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):             

#私钥需不需进行加密,设置密码

Enter same passphrase again:   

#私钥需不需进行加密,再次输入密码确认

Your identification has been saved in

 /root/.ssh/id_dsa.

Your public key has been saved in

 /root/.ssh/id_dsa.pub.
The key fingerprint is:
31:4a:4f:9f:97:b0:b6:ca:4c:53:78:70:89:83:5f:16 root@m01
The key's randomart image is:
+--[ DSA 1024]----+
|          E      |
|       . . o     |
|      o B *      |
|     . = @ + .   |
|      . S B o    |
|         + o     |
|        o .      |
|       + o       |
|        +        |
+-----------------+


02

分发公钥


[root@m01 ~]# ssh-copy-id  -i root/.ssh/id_dsa.pub  root@172.16.1.41


The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
RSA key fingerprint is 

d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c.

Are you sure you want to continue connecting (yes/no)? yes
Warning: 

Permanently added '172.16.1.41' (RSA) to the list of known hosts.

root@172.16.1.41's password:
Now try logging into the machine, with "ssh 'root@172.16.1.41'", and check in
 

.ssh/authorized_keys


to make sure we haven't added extra keys that you weren't expecting.


远程命令测试:
[root@m01 ~]# ssh root@172.16.1.41 "hostname -i"
172.16.1.41


03

安装ansible 


[root@m01 ~]# yum install ansible -y


04

进行修改


进行修改ansible下的hosts文件,注意文件的路径

[root@m01 ~]# vim /etc/ansible/hosts
[abc]
172.16.1.31
172.16.1.41
172.16.1.8


说明:

1.中括号中的名字代表组名
2.主机(hosts)部分可以使用域名、主机名、IP地址表示;一般此类配置中多使用IP地址;
3.组名下的主机地址就是ansible可以管理的地址


05

命令测试


[root@m01 ~]# ansible abc -m ping
172.16.1.8 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
172.16.1.41 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
172.16.1.31 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

说明:

-m 指定使用的模块
-a 指定使用模块中相应的命令参数,命令参数只能是基本命令,并不支持管道操作

all   为hosts文件中的组全部管理

未分发公钥如何实现远程管理主机及指定ansible端口信息
IP:端口 用户 密码(不建议,不安全,暴露了账号密码,内网安全风险较小时可以使用)

vim /etc/ansible/hosts

[abc]
172.16.1.31:52113  ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.41
172.16.1.8

结尾:Ansible   技能提高,模块调用,剧本编写,参考百度手册。

文章转载自浙金科技无限,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论