ubuntu24下安装ansible
环境:
OS:ubuntu 24.04
ansible:2.16.3
拓扑结构:
角色 IP地址 主机名
Ansible 服务端 192.168.1.100 master
客户端 1 192.168.1.102 node01
1.关闭防火墙
每台机器都要操作
sudo ufw disable
2.设置主机名
# 服务端执行
sudo hostnamectl set-hostname master
# 客户端1执行
sudo hostnamectl set-hostname node01
3.配置/etc/hosts
cat >> /etc/hosts << EOF
192.168.1.100 master
192.168.1.102 node01
EOF
4.安装ansible
只在master机器上执行
hxl@master:~$sudo apt update
hxl@master:~$sudo apt install ansible
查看安装的版本
hxl@master:~$ sudo ansible --version
ansible [core 2.16.3]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
5.服务端生成 SSH 密钥(实现免密登录客户端)
普通账号hxl下操作
#一路回车,无需设置密码
hxl@master:~$ 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:
SHA256:my3/U34VLh0AqO+3LM356ccaFRaAsM6pz50gbuBpuk8 root@master
The key's randomart image is:
+---[RSA 3072]----+
| .o.o... |
| ... . .|
| .. .o |
| .o . .o.|
| S+ o.o|
| . .= .oo.|
| .Eoo=.+ .+o .|
| .+..+=+=o +o.|
| o=... o+*=*o. |
+----[SHA256]-----+
分发公钥到客户端(服务端执行)
#分发到 node01,这里使用的是普通用户hxl
hxl@master:~$ ssh-copy-id hxl@node01
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
hxl@node01's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'hxl@node01'"
and check to make sure that only the key(s) you wanted were added.
6.免密验证(服务端测试)
hxl@master:~$ ssh node01 hostname
node01
7.服务端配置Ansible主机清单
自己单独创建ini文件,如下
sudo vi /tmp/inventory.ini
[mytest]
192.168.1.102
8.连通性测试
hxl@master:~$ ansible mytest -m ping -i /tmp/inventory.ini
192.168.1.102 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
9.应用
查看客户端根目录/目录的磁盘使用情况
方法1:
获取ip和usage
hxl@master:~$ ansible mytest -m shell -a 'df -h / | awk "NR==2{gsub(/%/,\"\",\$5);print \$5}"' -i /tmp/inventory.ini | awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {ip=$1;next}{if(ip!=""){print "IP: " ip " , usage: " $0;ip=""}}'
IP: 192.168.1.102 , usage: 46
方法2:
hxl@master:~$ more /tmp/test2.yml
---
- name: mytesthxl
hosts: mytest
gather_facts: false
tasks:
- shell: df -h / | awk 'NR==2{gsub(/%/,"",$5);print $5}'
register: disk_info
- shell: date "+%Y-%m-%d %H:%M:%S"
register: host_time
- set_fact:
result_line: "IP: {{ inventory_hostname }} , usage: {{ disk_info.stdout }} ,采集时间: {{ host_time.stdout }}"
- debug:
msg: "{{ result_line }}"
执行
hxl@master:~$ ansible-playbook /tmp/test2.yml -i /tmp/inventory.ini | grep msg | awk -F'"' '{print $4}'
IP: 192.168.1.102 , usage: 46 ,采集时间: 2026-07-30 17:46:37
浙公网安备 33010602011771号