创建inventory文件host
Ansible需要知道要连接到哪些服务器并管理这些服务器。
所以我们需要创建Ansibles host文件。
该文件包含服务器的列表,选项和组。
在MacOS X上,默认hosts文件为/usr/local/etc/ansible/hosts。
可以直接编辑文件/usr/local/etc/ansible/hosts或创建新的文件~/hosts:
$ vi ~/hosts
内容是服务器列表,每个服务器一行,可以是服务器名称或者IP
box1 192.168.2.15
确保在box1和192.168.2.15服务器中安装ssh密钥。
如果没有ssh密钥,在主节点(MacOS X)上生成ssh密钥:
$ ssh-keygen -t rsa -C "OS X laptop ssh key"
安装ssh密钥(将公钥复制到各个服务器中):
$ ssh-copy-id -i ~/.ssh/id_rsa.pub box1 $ ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.15
测试连接性:
$ ansible all --inventory-file=~/hosts -m ping
输出示例:
192.168.2.15 | SUCCESS => { "changed": false, "ping": "pong" } box1 | SUCCESS => { "changed": false, "ping": "pong" }
设置ANSIBLE_HOSTS变量来定义hosts文件。
这样,执行命令时,就不需要指定--inventory-file
$ export ANSIBLE_HOSTS=~/hosts $ echo 'export ANSIBLE_HOSTS=~/hosts' >> ~/.bashrc $ ansible all -m ping ## 在所有服务器上执行uptime命令: $ ansible all -a 'uptime'
输出示例:
192.168.2.15 | SUCCESS | rc=0 >> 01:20:36 up 10:06, 2 users, load average: 0.47, 0.46, 0.20 box1 | SUCCESS | rc=0 >> 19:50:41 up 58 min, 1 user, load average: 0.00, 0.00, 0.00
如何在运行MacOS X的Apple Macbook Pro上安装Ansible软件?
Ansible是简单易用的自动化不是工具,使用Ansible可以进行应用程序部署,配置管理,连续交付以及许多与sysadmin相关的任务。
使用brew命令在MacOS上安装Ansible
执行以下命令:
$ brew install ansible
Macbook pro现在是主节点或控制系统。
日期:2020-03-23 08:03:59 来源:oir作者:oir