随着分布式应用越来越热,微服务划分出更细,对应的部署服务器节点也越来越多,针对这些服务器,我们需要统一的管理,如果通过传统的人肉操作,不仅效率低,而且容易出错,维护困难,所以我们需要借助一个linux服务器管理工具,Ansible是一个不错的选择。

什么是Ansible

Ansible,自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

前置条件

以下命令使用的ansible版本为2.3.2.0,不保证其它版本兼容性

常用使用场景

批量ssh-copy-id

  1. 指定Hosts文件

假设文件位置为~/.ansible/hosts

1
2
3
[test-servers]
192.168.100.1 ansible_user=test ansible_ssh_pass="testPwd"
192.168.100.2 ansible_user=test ansible_ssh_pass="testPwd"

ansible_user即为你目标机器的用户,ansible_ssh_pass则为目标机器用户的登录密码

  1. 准备配置脚本

脚本文件push.ssh.yml内容:

1
2
3
4
5
6
7
- hosts: test-servers
user: test
tasks:
- name: ssh-copy
authorized_key: user=test key="{{ lookup('file', '/home/test/.ssh/id_rsa.pub') }}"
tags:
- sshkey
  1. 执行命令ansible-playbook -i ~/.ansible/hosts push.ssh.yml

批量修改密码

脚本文件change_password.yml内容:

1
2
3
4
5
- hosts: test-servers
gather_facts: false
tasks:
- name: Change password
user: name={{ targetUser }} password={{ newPwd | password_hash('sha512') }} update_password=always

执行命令ansible-playbook -i ~/.ansible/hosts change_password.yml -e "targetUser=test newPwd=admin#123"

执行默认模块

在使用ansible中的时候,默认的模块是-m command,直接使用即可。

ansible -i ~/.ansible/hosts test-servers -v -m ping -u root --private-key=~/.ssh/id_rsa

lineinfile模块

lineinfile:文件内容修改、在某行前面添加一行、在某行后面添加一行、删除某一行、末尾加入一行、替换或添加某一行

行为 命令
文件内容修改

ansible all -m lineinfile -a "dest=/root/test.txt regexp='bbb' line='bbbbbbb'"

在某一行前面插入一行

ansible all -m lineinfile -a "dest=/root/test.txt insertbefore='aa(.*)' line='eeee'"

在某一行后面插入一行

ansible all -m lineinfile -a "dest=/root/test.txt regexp='aa(.*)' state=absent"

末尾加入一行

ansible all -m lineinfile -a "dest=/root/test.txt line='hehe'"

允许指定IP进行ssh登录,编辑/etc/hosts.allow文件,在末尾加上sshd:192.168.2.1,192.168.1. 表示允许192.168.2.1这台机器和192.168.1.* 的所有机器ssh登录

替换或添加某一行

ansible all -m lineinfile -a "dest=/root/test.txt regexp='he(.*)' line='lllll' state=present"

删除某一行

ansible all -m lineinfile -a "dest=/root/test.txt regexp='xxxx' state='absent'"

file模块

删除文件或目录

ansible test-servers -m file -a "dest=/tmp/test.txt state=absent"

在目标机器上执行自定义命令

  • 使用 SSH-Key执行

ansible -i ~/.ansible/hosts test-servers -m command -a "date" -u test --private-key=~/.ssh/id_rsa

如果ssh private-key有密码,请加上参数 --ask-pass

传送文件

ansible test-servers -m copy -a "src=/tmp/source/test.sh dest=/tmp/target/"

其中/tmp/source/test.sh为本地文件, /tmp/target/为目标文件夹,如果文件夹为空会报错

批量修改HostName

playbook:change_hostname.yml

1
2
3
4
5
6
7
8
- hosts : test-servers
remote_user : root
tasks :
- name : show hostname
shell : hostname
- name : show ip
command : ip a
- hostname : name=test-server-{{ ansible_default_ipv4.address.split('.')[-1] }}

执行命令ansible-playbook -i ~/.ansible/hosts test-servers change_hostname.yml -u root --private-key=~/.ssh/id_rsa --ask-pass


本文会持续更新




如果您觉得这篇文章对您有所帮助, 点我, 可以请我喝杯咖啡。
< 支付宝 | 微信 >
Published with Hexo and Theme by Kael
Flag Counter
X