ansible 安装配置
### ansible 组成:
~~~
Control Node:控制机器
Inventory:主机清单,配置管理主机列表
Playbooks:剧本、任务编排。根据规则定义多个任务,模块组织结构清晰,由ansible自动执行。
Modules(Core | Custom):模块,用于执行某个具体的任务
connection plugin(连接插件):Ansible通过不同的协议连接到远程主机上,执行指定的命令。默认采用ssh协议连接远程主机。
~~~
### 特性
~~~
no agents:不需要在被管控主机上安装任何客
no server:无服务器端,使用时直接运行命令
modules in any languages:基于模块工作,可使用任意语言开发
yaml,not code:使用yaml语言定制剧本playbo
ssh by default:基于SSH
strong multi-tier solution:可实现多级
~~~
### 优点
~~~
轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
使用python编写,维护更简单,ruby语法过于复杂;
支持sudo。
~~~
### 运行:
Ansible在运行时,首先读取ansible.cfg中的配置,根据规则获取Inventory中的管理主机
列表,并行的在这些主机中执行配置的任务,最后等待执行返回的结果。
### 安装:
一台控制主机:10.0.0.200
三台管理主机:10.0.0.201
10.0.0.202
10.0.0.203
### 安装要求:
控制服务器:需要安装 Python2.6/2.7
管理服务器:需要安装 Python2.4 以上版本,若低于 Python2.5 需要安装 pythonsimplejson; 若启用了 selinux,则需要安装 libselinux-python。
#### yum安装(推荐)
~~~
yum install epel-release
yum install ansible
~~~
#### pip安装
~~~
pip install ansible
~~~
注:pip方式安装不会在/etc/ansible目录下生成默认的相关配置文件
#### 配置管理主机
~~~~
vim /etc/ansible/hosts
~~~~
#### 在hosts文件中添加管理主机的IP地址列表
~~~~
10.0.0.201
10.0.0.202
10.0.0.203
~~~~
#### 配置控制主机ssh密钥
#### 再控制主机中生成ssh密钥对
ssh-keygen -t rsa
一路回车即可在$HOME/.ssh目录下生成id_rsa和id_rsa.put私钥和公钥两个文件。
注: 如果在生成密钥的时候设置了密码,ansible每次执行命令的时候,都会提示输入密钥密码,可通过下面的命令记住密码。
ssh-agent bsh
ssh-add ~/.ssh/id_rsa
将公钥拷贝到管理主机中.ssh/authorized_keys文件中,实现免密码登录远程管理主机
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.201
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.202
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.203
注:ssh-copy-id命令会自动将id_rsa.pub文件的内容追加到远程主机root用户下.ssh/authorized_keys文件中。
### ansible配置
vim /etc/ansible/ansible.cfg
1> 禁用每次执行ansbile命令检查ssh key host
host_key_checking = False
2> 开启日志记录
log_path = /var/log/ansible.log
### 测试
#### 最后测试
### 在三台主机创建一个文件 ceshi
[root@jinc2 ~]# ansible all -a "touch /mnt/ceshi"
[WARNING]: Consider using the file module with state=touch rather than running
touch. If you need to use command because file is insufficient you can add
warn=False to this command task or set command_warnings=False in ansible.cfg to
get rid of this message.
10.0.0.203 | SUCCESS | rc=0 >>
10.0.0.201 | SUCCESS | rc=0 >>
10.0.0.200 | SUCCESS | rc=0 >>
##### 控制端成功后显示以上
##### 主机:10.0.0.200
[root@jinc mnt]# ls
1.txt jdk-10.0.2_linux-x64_bin.tar.gz
2.txt Linux常用命令全集.chw
ceshi
file3
##### 主机:10.0.0.201
[root@jinc1 mnt]# ls
ceshi hgfs oldboy
##### 主机:10.0.0.203
[root@jinc2 mnt]# ls
1.txt jdk-10.0.2_linux-x64_bin.tar.gz
2.txt Linux常用命令全集.chw
ceshi
file3
##### 在此我们发现都有了ceshi 这个文件,到此,告一段落。