ansible是一个由 Python 编写的强大的配置管理解决方案。ansible 的特点就在于它的简洁。让 ansible 在主流的配置管理系统中与众不同的一点便是,它并不需要你在想要配置的每个节点上安装自己的组件。同时提供的一个优点在于,如果需要的话,你可以在不止一个地方控制你的整个基础架构。

安装

只需要在控制端安装即可。

# Centos
yum install epel-release
yum install ansible -y

# Ubunut
apt-get install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get update
apt-get install ansible

# pip安装
pip install ansible


# 安装最新版
cd ~
git clone git://github.com/ansible/ansible.git
cd ./ansible
source ./hacking/env-setup
# 加入环境变量(用git才需要修改环境变量)
echo "export ANSIBLE_HOSTS=~/ansible_hosts" >> ~/.bashrc     #建立自己的hosts文件,这样方便管理
echo "source ~/ansible/hacking/env-setup" >> ~/.bashrc       #github ansible目录下的环境配置文件

配置

# 编辑hosts文件,加入主机ip、域等信息
vim /etc/ansible/hosts

# 测试ping,会提示输入登录密码。
ansible all -m ping --ask-pass -c paramiko

# 分发密钥;如果是第一次连接主机分发不成功,可以手动连接一次,再分发密钥。
# 生成密钥
ssh-keygen -t rsa
# 分发密钥
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko

#密钥分发完成后再次测试,这时不用带--ask-pass -c paramiko了,也不用输入密码了。
ansible all -m shell -a "hostname" -u root

简单 Ansible 命令

命令格式ansible <group> -m <module> -a <arguments>
可以用单个主机或用 代替一组主机, 是可选的参数。

# 检查主机的连通性
ansible <group> -m ping

# 重启主机
ansible <group> -a "/sbin/reboot"

# 检查主机的系统信息
ansible <group> -m setup | less
# 还可以通过传递参数来从收集的信息中检查特定的信息:
ansible <group> -m setup -a "filter=ansible_distribution"

传输文件
对于传输文件,使用模块 copy ,完整的命令是这样的:

ansible <group> -m copy -a "src=/home/leo dest=/tmp/home"

管理用户
要管理已连接主机上的用户,可以使用一个名为 user 的模块。

# 创建新用户
ansible <group> -m user -a "name=testuser01 password=<encrypted password>"

# 删除用户
ansible <group> -m user -a "name=testuser01 state=absent"

注意: 要创建加密密码,请使用 "mkpasswd -method=sha-512"

更改权限和所有者
要改变已连接主机文件的所有者,可以使用名为 file 的模块。

# 更改文件权限
ansible <group> -m file -a "dest=/home/leo/file1.txt mode=777"

# 更改文件的所有者
ansible <group> -m file -a "dest=/home/leo/file1.txt mode=777 owner=leo group=leo"

管理软件包
可以使用 yum 和 apt 模块来管理所有已连接主机的软件包。

# 检查包是否已安装并更新
ansible <group> -m yum -a "name=ntp state=latest"

# 检查包是否已安装,但不更新
ansible <group> -m yum -a "name=ntp state=present"

# 检查包是否是特定的版本
ansible <group> -m yum -a "name= ntp-1.8 state=present"

# 检查包是否没有安装
ansible <group> -m yum -a "name=ntp state=absent"

管理服务
要管理服务,可以使用 service 模块。

# 启动服务
ansible <group> -m service -a "name=httpd state=started"

# 停止服务
ansible <group> -m service -a "name=httpd state=stopped"

# 重启服务
ansible <group> -m service -a "name=httpd state=restarted"


批量(分组)操作主机

# 先给主机分组(按系统分,按客户分、按职能分)
[Centos]
127.0.0.1
192.168.10.34

[Ubuntu]
192.168.10.38

# 创建yaml格式的playbook文件

vim testPlaybook.yaml
# ansible命令格式中,-m 是一个模块,而 -a 用来提供模块参数。
# 在 YAML 表示中你可以先指定模块,然后插入一个冒号“:”最后指定参数,文件头“---”不能少。

---
- hosts: Ubuntu
  remote_user: root
  tasks:
    - apt: name=apache2 state=latest
- hosts: Centos
  remote_user: root
  tasks:
    - yum: name=httpd state=latest


# 批量执行(可以把各种批量操作,部署环境,新增主机等等写成yaml格式的Playbook,加入git,就可以方便管理了)
ansible-playbook testPlaybook.yaml -f 10     # -f 参数让 ansible 在多台主机上同时运行指令。

参考:https://linux.cn/article-4215-3.html

参考:
https://linux.cn/article-9527-1.html
https://linux.cn/article-9466-1.html
https://linux.cn/article-4215-1.html
http://www.ansible.com.cn
http://blog.51cto.com/sofar/1579894

文章作者: Leo
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 LeoLan的小站
系统管理 ansible 自动化运维
喜欢就支持一下吧