Ansible学习笔记2

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

特点:

1)部署简单;

2)默认使用ssh进行管理,基于Python里的paramiko模块开发;

3)管理端和被管理端不需要启动服务;

4)配置简单、功能强大、拓展性强;

5)能playbook剧本进行多任务的编排;

实验准备:

三台机器:一台管理、两台被管理。

|-----------|----------------|----------|
| 主机 | IP地址 | 备注信息 |
| 管理机Master | 192.168.17.104 | |
| 被管理机1 | 192.168.17.105 | |
| 被管理机2 | 192.168.17.106 | |

实验过程:

1)在管理机上安装ansible,被管理机上必须打开ssh服务。

bash 复制代码
yum install -y epel-release
yum install ansible

[root@localhost ~]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 20 2023, 11:36:40) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

2)实现master对agent的免密登录,只有master上做。(如果这步不做的话,则在后面操作agent时候要加上-k参数传密码,或者在主机清单里传密码。)

bash 复制代码
ssh-keygen

ssh-copy-id -i 192.168.17.105
ssh-copy-id -i 192.168.17.106

3)在master上定义主机组,并测试连接性。

说明:inventory:财产清单,库存等。

在hosts清单中添加两个机器IP地址。

bash 复制代码
[root@localhost ansible]# tail -3 hosts
[group1]
192.168.17.105
192.168.17.106
bash 复制代码
[root@localhost ansible]# ansible all -m ping
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.17.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

说明:all是所有机器。-m是module的意思,ping是模块。

bash 复制代码
[root@localhost ansible]# ansible group1 -m ping
192.168.17.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

服务器分组:

ansible通过一个主机清单功能来实现服务器分组。

Ansible的默认主机清单配置文件为/etc/ansible/hosts

另外,如果服务器比较多的情况下,也可以写:

bash 复制代码
[nginx]
apache[1:10].aaa.com
nginx[a:z].aaa.com
10.1.1.[11:15]
bash 复制代码
[root@localhost ansible]# tail -2 hosts
[group1]
192.168.17.[105:106]

然后再用ping模块测试:

bash 复制代码
[root@localhost ansible]# ansible group1 -m ping
192.168.17.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

再看一个例子:

bash 复制代码
[nginx]
10.1.1.13:2222

表示的是10.1.1.13这台机器,但是端口是2222端口。

bash 复制代码
[root@localhost ansible]# tail -3 hosts
[group1]
192.168.17.105
192.168.17.106:2222
bash 复制代码
[root@localhost ansible]# ansible group1 -m ping
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.17.106 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

别名的写法:

定义10.1.1.12:2222 这台服务器的别名nginx1。

bash 复制代码
nginx1 ansible_ssh_hosts=10.1.1.13 ansible_ssh_port=2222

没有做免密登录的服务器可以指定用户名和密码

bash 复制代码
nginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx2 ansible_ssh_host=10.1.1.12


# /etc/ansible/hosts
[nginx]
nginx1
nginx2

说明:密码最好是一个加密字符串。

bash 复制代码
[root@localhost ansible]# tail -6 hosts
nginx1 ansible_ssh_host=192.168.17.106 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="1q2w3e4r"

## db-[99:101]-node.example.com
[group1]
192.168.17.105
nginx1

别名的测试。

bash 复制代码
[root@localhost ansible]# ansible group1 -m ping
192.168.17.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
nginx1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

小结:

主机清单的作用:服务器分组。主机清单就是服务器分组。

主机清单的常见功能:

1)可以通过IP范围来分,主机名的名称来分;

2)如果ssh端口不是22,可以传入新端口;

3)没有做免密登录,可以传密码。

不论是哪种环境(免密或不免密,端口是否22),请最终将两台被管理机器加入到group1组即可。

相关推荐
wingaso8 分钟前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
独行soc12 分钟前
2025年渗透测试面试题总结-阿里云[实习]阿里云安全-安全工程师(题目+回答)
linux·经验分享·安全·阿里云·面试·职场和发展·云计算
勤不了一点24 分钟前
小白上手RPM包制作
linux·运维·服务器·软件工程
麦a~M了M2 小时前
ansible
linux·运维·ansible
QQ_4376643143 小时前
Linux下可执行程序的生成和运行详解(编译链接汇编图解)
linux·运维·c语言·汇编·caffe
窦再兴4 小时前
来一个复古的技术FTP
linux·运维·服务器
xiaobin889994 小时前
【2025最新版】VMware虚拟机下载安装教程 保姆级图文详解(附安装包+常用镜像Linux,win11,ubuntu,centos)
linux·其他·ubuntu·centos
ALex_zry4 小时前
Ubuntu 20.04 C++开发环境搭建指南(2025版)
linux·c++·ubuntu
疯狂的挖掘机5 小时前
记一次从windows连接远程Linux系统来控制设备采集数据方法
linux·运维·windows
sz66cm6 小时前
Linux基础 -- 用户态Generic Netlink库高性能接收与回调框架
linux