Centos7安装ansible自动化运维工具

一、安装ansible软件

介绍Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务。
Ansible的特点
1、ansible不需要单独安装客户端,也不需要启动任何服务
2、ansible是python中的一套完整的自动化执行任务模块
3、ansible playbook 采用yaml配置,对于自动化任务执行过一目了然
Ansible组成结构

  • Ansible
    是Ansible的命令工具,核心执行工具;一次性或临时执行的操作都是通过该命令执行。
  • Ansible Playbook
    任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,yaml格式。
  • Inventory
    Ansible管理主机的清单,默认是/etc/ansible/hosts文件。
  • Modules
    Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。
  • Plugins
    插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。
  • API
    提供给第三方程序调用的应用程序编程接口。

1、安装yum源

rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm
yum install epel-release -y
yum install ansible

要是报错:epel源与python版本冲突原因,有些包是需要依赖python2.6的版本,此主机的python版本是2.7.5。

2、那就先卸载 epel-release源

yum install epel-release -y

3、到 /etc/yum.repos.d 目录下,将epel.repo源备份

mv epel.repo epel.repo.bak

4、清理yum源缓存和新建缓存

yum clean all 
yum makecache

5、再执行安装命令

yum install ansible -y

6、查看安装的版本

ansible --version

7、配置主机组

Ansible工具默认主目录为/etc/ansible/,其中hosts文件为被管理机IP或者主机名列

图片[1]众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站

二、配置免秘钥登录使用

1、管理主机上生成秘钥

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
73:80:07:fa:9a:0d:e0:0e:d1:c2:44:d2:d2:61:67:21 root@ansible
The key's randomart image is:
+--[ RSA 2048]----+
|o=E.+..          |
|=oo+ . o         |
|ooo . . o        |
| + . . . .       |
|. . . . S .      |
| o   =   o       |
|  . o .          |
|                 |
|                 |
+-----------------+

2、将管理机上生成的秘钥发送到被管理机

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.112.130
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.223.131

3、配置连接

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密钥
[webserver]
192.168.1.31:22
192.168.1.32
192.168.1.33
192.168.1.36

# 方法一 别名主机+端口+密钥
[webserver]
node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22
node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22
node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22
node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22

4、测试是否配置成功

ansible -k all -m ping

三、基于密码连接使用

# 方法一 主机+端口+密码
[webserver]
192.168.100.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.100.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.100.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.100.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

# 方法二 主机+密码
[webserver]
192.168.100.3[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"

# 方法二 主机+端口+密码
[webserver]
192.168.100.3[1:3]
[webserver:vars]
ansible_ssh_pass="123456"

四、主机组的使用

# 主机组变量名+主机+密码
[apache]
192.168.100.36
192.168.100.33
[apache.vars]
ansible_ssh_pass='123456'

# 主机组变量名+主机+密钥
[nginx]
192.168.100.3[1:2]

# 定义多个组,把一个组当另外一个组的组员
[webserver:children]  #webserver组包括两个子组:apache nginx
apache
nginx

五、临时指定inventory

1)先编辑一个主机定义清单

[root@ansible ~]# vim /etc/dockers
[dockers]
192.168.100.31 ansible_ssh_pass='123456'
192.168.100.32
192.168.100.33

2)在执行命令是指定inventory

[root@ansible ~]# ansible dockers -m ping -i /etc/dockers -o 
192.168.100.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.100.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.100.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

六、Inventory内置参数

图片[2]众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站

七、Ansible Ad-Hoc

ad-hoc临时的,在ansible中是指需要快速执行,并且不需要保存的命令。说白了就是执行简单的命令——一条命令。对于复杂的命令则为playbook,类似于saltstack的state sls状态文件。
ansible命令格式#
1)常用命令参数

[root@ansible ~]# ansible -h
Usage: ansible [options]

-a MODULE_ARGS #模块参数
-C, –check #检查语法
-f FORKS #并发
–list-hosts #列出主机列表
-m MODULE_NAME #模块名字
-o 使用精简的输出

2)示例

[root@ansible ~]# ansible webserver -m shell -a 'uptime' -o
192.168.1.36 | CHANGED | rc=0 | (stdout)  13:46:14 up 1 day,  9:20,  4 users,  load average: 0.00, 0.00, 0.00
192.168.1.33 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:51,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.31 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:50,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.32 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:59,  3 users,  load average: 0.00, 0.01, 0.05

3)命令说明图片[3]众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站众客华禹 – 网站运维分享-IT技术资源教程-运维成长之路-个人随笔-华禹个人博客网站

host-pattern格式

目标target主机,主机组匹配方式

1、主机的匹配

#  一台目标主机
[root@ansible ~]# ansible 192.168.100.31 -m ping
# 多台目标主机
[root@ansible ~]# ansible 192.168.100.31,192.168.100.32 -m ping
# 所有目标主机
[root@ansible ~]# ansible all -m ping

2、组的匹配

# 组的配置信息如下:这里定义了一个nginx组和一个apache组
[root@ansible ~]# ansible nginx --list
  hosts (2):
    192.168.1.31
    192.168.1.32
[root@ansible ~]# ansible apache --list
  hosts (3):
    192.168.1.36
    192.168.1.33
    192.168.1.32

# 一个组的所有主机匹配
[root@ansible ~]# ansible apache -m ping

# 匹配apache组中有,但是nginx组中没有的所有主机
[root@ansible ~]# ansible 'apache:!nginx' -m ping -o
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache组和nginx组中都有的机器(并集)
[root@ansible ~]# ansible 'apache:&nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache组nginx组两个组所有的机器(并集);等于ansible apache,nginx -m ping
[root@ansible ~]# ansible 'apache:nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 共1条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片