centos7下安装ansible
阅读原文时间:2023年07月11日阅读:1

由于centos7预装了python,因此我们可以跳过python的安装环节(记得关闭防火墙)

[root@model ~]#
[root@model ~]# python --version
Python 2.7.
[root@model ~]#

本文使用yum安装ansible

[root@model ~]# yum install epel-release

[root@model ~]# yum install ansible

配置目标服务器192.168.1.10和192.168.1.20(在文件中添加红色部分)

[root@model ~]# vi /etc/ansible/hosts

This is the default ansible 'hosts' file.

It should live in /etc/ansible/hosts

- Comments begin with the '#' character

- Blank lines are ignored

- Groups of hosts are delimited by [header] elements

- You can enter hostnames or ip addresses

- A hostname/ip can be a member of multiple groups

Ex : Ungrouped hosts, specify before any group headers.

green.example.com

blue.example.com

192.168.100.1

192.168.100.10

Ex : A collection of hosts belonging to the 'webservers' group

[webservers]

alpha.example.org

beta.example.org

192.168.1.100

192.168.1.110

If you have multiple hosts following a pattern you can specify

them like this:

www[:].example.com

Ex : A collection of database servers in the 'dbservers' group

[dbservers]

db01.intranet.mydomain.net

db02.intranet.mydomain.net

10.25.1.56

10.25.1.57

Here's another example of host ranges, this time there are no

leading 0s:

db-[:]-node.example.com

192.168.1.10
192.168.1.20

~
~
~
~
~
~
"/etc/ansible/hosts" 47L, 1041C

生成SSH密钥

[root@model ~]# ssh-keygen -t rsa

将/root/.ssh目录下的id_rsa.pub拷贝到目标服务器

[root@model ~]# scp /root/.ssh/id_rsa.pub root@192.168.1.10:/root/.ssh/authorized_keys

[root@model ~]# scp /root/.ssh/id_rsa.pub root@192.168.1.20:/root/.ssh/authorized_keys

测试批量执行ping命令

[root@model ~]#
[root@model ~]# ansible all -m ping
192.168.1.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@model ~]#

至此,ansible安装完成~