ansible的使用

ansible

1.首先安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1.安装
[root@ansible:~]#apt install -y python3-pip install sshpass # ansible基于密码连接需要使用
[root@ansible:~]#sudo apt install -y python3-pip #python3-pip 是安装 ansible 的「其中一种工具」 (可选) --pip3 install ansible--✅ 必须先装 python3-pip--可装最新版本 / 指定版本--容易和系统 Python 依赖冲突
[root@ansible:~]#sudo apt install -y ansible (可选) 用ubuntu直接下载--和Ubuntu 系统兼容,无依赖冲突--版本可能不是最新
[root@ansible ~]# ansible --version #查看是否安装成功
2.创建ansible目录
[root@ansible:~]#mkdir /etc/ansible
3.创建配置文件
[root@ansible:~]#cat /etc/ansible/ansible.cfg
[defaults]
host_key_checking = False
deprecation_warnings = False
interpreter_python = /usr/bin/python3
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
#centos直接使用即可,省略以上步骤

2.Inventory主机清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
1.基于秘钥方式管理客户端 
root@ansible ~]# ssh-keygen
enerating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #让你选择密钥文件的保存位置,默认路径是 /root/.ssh/id_rsa
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y #因为我已经有了,这是询问是否覆盖之前的
Enter passphrase (empty for no passphrase): #你可以直接按回车,生成无密码的密钥(适合 Ansible 批量自动化场景);也可以设置一个密码,增加安全性(但每次使用都需要输入密码,自动化场景不推荐)。
Enter same passphrase again: #直接按回车(推荐用于 Ansible 自动化场景)

2.将公钥拷贝到目标主机
[root@ansible:~]#ssh-copy-id 10.0.0.41
[root@ansible:~]#ssh-copy-id 10.0.0.42
[root@ansible:~]#ssh-copy-id 10.0.0.43
登录测试:是否成功
[root@ansible:~]#ssh 10.0.0.41

3.配置主机清单 #自己设置
[root@ansible:~]#cat /etc/ansible/hosts
10.0.0.41
10.0.0.42
10.0.0.43
测试
[root@ansible ~]# ansible 10.0.0.41 -m ping
10.0.0.41 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible 10.0.0.42 -m ping
10.0.0.42 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@ansible ~]# ansible 10.0.0.43 -m ping
10.0.0.43 | SUCCESS => {
"changed": false,
"ping": "pong"
}

#别名配置、组的配置
[root@ansible:~]#cat /etc/ansible/hosts
root@ansible ~]# cat /etc/ansible/hosts
backup ansible_ssh_host=10.0.0.41

[webs]
web01 ansible_ssh_host=10.0.0.41
web02 ansible_ssh_host=10.0.0.42
web03 ansible_ssh_host=10.0.0.43


[root@ansible:~]#ansible web02 -m ping
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@ansible:~]#ansible web03 -m ping
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@ansible:~]#ansible webs -m ping
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}



配置多个组属于一个组(lnmp组包含了back组和webs组)
[root@ansible:~]#cat /etc/ansible/hosts
root@ansible ~]# cat /etc/ansible/hosts
[back]
backup ansible_ssh_host=10.0.0.41

[webs]
web01 ansible_ssh_host=10.0.0.41
web02 ansible_ssh_host=10.0.0.42
web03 ansible_ssh_host=10.0.0.43

[lnmp:children]
back
webs

3.Ad-hoc

1.模块yum/apt

1
2
作用: 基于ansibl模块执行单条命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装wget命令
[root@ansible:~]#ansible web01 -m apt -a 'name=wget state=present'

# 卸载wget命令
[root@ansible:~]#ansible web01 -m apt -a 'name=wget state=absent'


黄色表示已经执行的命令。
红色表示执行失败。

控制webs组安装wget
[root@ansible:~]#ansible webs -m apt -a 'name=wget state=present'


2.模块file

1
2
作用: 创建普通文件和目录

3.模块copy

1
2
作用: 远程拷贝,将ansible上的文件拷贝到目标主机

4.命令模块 不推荐

1
2
3
4
5
6
7
```

### 5.模块user

```bash
作用: 创建用户删除用户

6.模块cron定时任务

1
2
作用: 配置定时任务

7.模块systemd

1
2
作用: 启动停止管理服务

8.模块get_url

1
2
作用: 下载软件
[root@ansible:~]#ansible web01 -m get_url -a 'url=https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.93/bin/apache-tomcat-9.0.93.tar.gz dest=/root/'

9.模块mount

1
2
作用: 挂载


ansible的使用
http://example.com/2025/05/16/ansible的使用/
作者
发布于
2025年5月16日
许可协议