2024年Linux最新自动化运维神器Ansible_千锋ansible

本文介绍了如何使用Ansible进行自动化运维,包括安装、配置ssh公钥认证、查看主机列表、使用不同模块如ping和shell执行命令。通过示例展示了如何管理主机组,以及Ansible在检查主机连接性、执行shell命令和管理用户等方面的用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

192.168.1.11 ansible-web2
192.168.1.12 ansible-web3
192.168.1.9 ansible-server (控制节点服务器端)
配置ssh公钥认证:控制节点需要发送ssh公钥给所有非被控制节点
[root@ansible-server ~]# ssh-keygen
[root@ansible-server ~]# ssh-copy-id -i 192.168.1.10 #所有机器


#### 2、安装



安装:控制节点

  1. 配置EPEL网络yum源
    [root@ansible-server ~]# yum install -y epel*
  2. 安装ansible
    [root@ansible-server ~]# yum install -y ansible
    3.查看版本
    [root@ansiable-server ~]# ansible --version
    ansible 2.8.4
    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, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
    4.看帮助
    [root@ansible-server ~]# ansible --help

#### 3、ansible基础----inventory主机清单


官方文档: [http://docs.ansible.com/ansible/intro\_inventory.html#](https://bbs.csdn.net/topics/618542503)


**inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、密码以及key相关信息。**



查看配置文件:
[root@ansible-server ~]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
-q:—query查询
1.主配置文件:
/etc/ansible/ansible.cfg #主要设置一些ansible初始化的信息,比如日志存放路径、模块、插件等配置信息
2.主机清单文件:
默认位置/etc/ansible/hosts
语法:
1.添加主机或者主机组:
[root@ansible-server ~]# vim /etc/ansible/hosts #在最后追加被管理端的机器
ansible-web1 #单独指定主机,可以使用主机名称或IP地址
2.添加主机组:
[webservers] #使用[]标签指定主机组 ----标签自定义
192.168.10.11 #如果未解析添加ip
ansible-web2 #解析添加主机名
3.组可以包含其他组:
[webservers1] #组一
ansible-web1
[webservers2] #组二
ansible-web2
[weball:children] #caildren-照写 #weball包括两个子组
webservers1 #组一
webservers2 #组二
4.为一个组指定变量,组内每个主机都可以使用该变量:
[weball:vars] #设置变量,vars–照写
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
#ansible_ssh_pass=1 #也可以定义密码,如果没有互传秘钥可以使用密码。


Ansible Inventory 常见的内置参数:


![](https://img-blog.csdnimg.cn/img_convert/bc1f8c194f106f30380f87ee0331bb6f.png)



查看组内主机列表:
语法:ansible 组名 --list-hosts
[root@ansible-server ~]# ansible weball --list-hosts
hosts (2):
ansible-web1
ansible-web2

扩展:自定义主机列表使用密码登录:(了解)
[root@ansible-server ~]# vim /opt/hostlist
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
#ansible_ssh_private_key_file=/root/.ssh/id_rsa
a1nsible_ssh_pass=1

[all]
ansible-web1
ansible-web2
使用:
[root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
-i:指定清单文件
注意:这里的ping并不是真正意义上的ping而是探测远程主机ssh是否可以连接!判断ssh端口是否存活


#### 4、测试



语法:

ansible -m <module_name> -a

pattern–主机清单里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
-m module_name: 模块名称,默认为command
-a arguments: 传递给模块的参数
-o 横着显示(单行显示)


使用案例:



使用ping模块检查ansible节点的连通性:
1.指定单台机器:
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
ansible-web1 | SUCCESS => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “ping”: “pong”}
2.同时指定多台机器:
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
ansible-web1 | SUCCESS => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “ping”: “pong”}
ansible-web2 | SUCCESS => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “ping”: “pong”}
3.指定组名:
[root@ansible-server ~]# ansible webservers1 -m ping -o
ansible-web1 | SUCCESS => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “ping”: “pong”}
执行shell命令:
[root@ansible-server ~]# ansible webservers1 -m shell -a ‘uptime’
ansible-web1 | CHANGED | rc=0 >>
23:32:47 up 5:24, 3 users, load average: 0.00, 0.01, 0.05
不加 -m 默认是 command 模块
[root@ansible-server ~]# ansible webservers1 -a ‘uptime’
ansible-web1 | CHANGED | rc=0 >>
23:34:01 up 5:25, 3 users, load average: 0.16, 0.05, 0.06
1.给节点增加用户:
[root@ansible-server ~]# ansible webservers1 -m shell -a ‘useradd tom’
ansible-web1 | CHANGED | rc=0 >>
[root@ansible-server ~]# ansible webservers1 -a ‘grep tom /etc/passwd’
ansible-web1 | CHANGED | rc=0 >>
tom❌1000:1000::/home/tom:/bin/bash
重定向输出到本地文件中:
[root@ansible-server ~]# ansible webservers1 -a ‘df -Th’ > /opt/a.txt
[root@ansible-server ~]# cat /opt/a.txt
ansible-web1 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 18G 1.1G 17G 6% /
devtmpfs devtmpfs 226M 0 226M 0% /dev
tmpfs tmpfs 237M 0 237M 0% /dev/shm
tmpfs tmpfs 237M 4.7M 232M 2% /run
tmpfs tmpfs 237M 0 237M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 125M 890M 13% /boot
tmpfs tmpfs 48M 0 48M 0% /run/user/0
面试:
在ansible中shell 和 command 模块. 这两个模块在很多情况下都能完成同样的工作,那么这两个模块之间的区别是什么?


#### 5、Ad-Hoc


ad hoc其实就是执行简单的命令——一条命令。对于复杂的命令则为 playbook。



帮助文档:
列出ansible支持的模块:
-l:获取列表
-s module_name:获取指定模块的使用信息
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l
查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值