官方文档https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html

状态

红色 执行失败

绿色 执行成功,状态与预期相符

安装

只需要在控制节点安装即可

Debian

apt install wger gpg 
添加储存库
UBUNTU_CODENAME=jammy
wget -O- "https://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=get&search=0x6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367" | sudo gpg --dearmour -o /usr/share/keyrings/ansible-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/ansible-archive-keyring.gpg] http://ppa.launchpad.net/ansible/ansible/ubuntu $UBUNTU_CODENAME main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt update && sudo apt install ansible

配置文件

在控制节点

cd /etc/ansible
root@debian:/etc/ansible# ls -l
total 8
-rw-r--r-- 1 root root 665 Aug 29 21:22 ansible.cfg
-rw-r--r-- 1 root root 122 Aug 29 21:24 hosts.yaml

#hosts存放为受管节点的ip或域名
root@debian:/etc/ansible# cat hosts.yaml
all:
  children:
    webserver:
      hosts:
        192.168.100.176:
    dbserver:
      hosts:
        192.168.100.177:
#ansible.cfg 存放指定的文件路径
root@debian:/etc/ansible# cat ansible.cfg
[defaults]
inventory = /etc/ansible/hosts.yaml
#

生成控制节点的公私钥

root@debian:~# ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Enter passphrase for "/root/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519
Your public key has been saved in /root/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:2kamuQRYshU1ri5g/4a+FPGsWjnqJf2O6AEK+3NPi6I root@debian
The key's randomart image is:
+--[ED25519 256]--+
|    ..o          |
|     o .         |
|  ..o .          |
|   *+.           |
|+.o.oo  S        |
|=oo.+. B         |
|oo.Xo * o        |
| .%o*= +         |
|EBoB==+          |
+----[SHA256]-----+
root@debian:~#

ssh 受管节点

ssh 192.168.100.176
root@debian:/etc/ansible# ssh 192.168.100.177
[email protected]'s password:
Linux debian 6.1.0-25-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26) x86_64
## 这里会弹一个是否信任的消息 输入yes

exit
#记得退出来
#复制自己的公钥到管控节点

root@debian:~# ssh-copy-id 192.168.100.177
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_ed                                                                                           25519.pub"
The authenticity of host '192.168.100.177 (192.168.100.177)' can't be establish                                                                                           ed.
ED25519 key fingerprint is SHA256:mp2uV3lPkwj3Bnob+CLKo/vAZ8Hhk/jt2jSmGQD2abc.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: [hashed name]
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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 promp                                                                                           ted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '192.168.100.177'"
and check to make sure that only the key(s) you wanted were added.
#另一个节点同理
root@debian:~#

root@debian:/etc/ansible# ansible webserver -m ping
[WARNING]: Host '192.168.100.176' is using the discovered Python interpreter at '/usr/bin/python3.13', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/devel/reference_appendices/interpreter_discovery.html for more information.
192.168.100.176 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.13"
    },
    "changed": false,
    "ping": "pong"
}

root@debian:/etc/ansible# ansible dbserver -m ping
[WARNING]: Host '192.168.100.177' is using the discovered Python interpreter at '/usr/bin/python3.13', but future installation of another Python interpreter could cause a different interpreter to be discovered. See https://docs.ansible.com/ansible-core/devel/reference_appendices/interpreter_discovery.html for more information.
192.168.100.177 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.13"
    },
    "changed": false,
    "ping": "pong"
}

成功