Skip to content

Commit 7afc347

Browse files
committed
Updated 'src/usage/playbook/executing/become.md'.
1 parent 275c80e commit 7afc347

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

playbook_executing/foo.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
gather_facts: no
33

44
tasks:
5-
- file:
6-
path: /tmp/myconfig.conf
7-
state: touch
5+
- name: Ensure the nginx service is running
6+
service:
7+
name: nginx
8+
state: started
9+
become: true
810

9-
- name: This task will never make changes to the system
10-
ansible.builtin.lineinfile:
11-
line: "important config"
12-
dest: /tmp/myconfig.conf
13-
state: present
14-
register: changes_to_important_config
11+
- name: Run a command as the nginx user
12+
command: nginx -t
13+
become: true
14+
become_user: nginx

src/usage/playbook/executing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
准备好运行咱们的 Ansible playbook 了吗?
55

66

7-
运行复杂 playbook 需要反复试错,因此就要了解 Ansible 给到咱们的一些功能,以确保成功的执行。咱们可以 “模拟运行,dry run” playbook,来验证咱们的任务,使用 start-at-task 与单步模式等选项,来有效地排除 playbook 问题。咱们还可使用 Ansible 的调试器,在执行过程中纠正任务。通过异步的 playbook 执行,Ansible 提供了灵活性,并提供了允许咱们运行咱们 playbook 特定部分的标签特性。
7+
运行复杂 playbook 需要反复试错,因此就要了解 Ansible 给到咱们的一些功能,以确保成功的执行。咱们可以 “模拟运行,dry run” playbook,来验证咱们的任务,使用 `--start-at-task``--step` 单步模式等选项,来有效地排除 playbook 问题。咱们还可使用 Ansible 的调试器,在执行过程中纠正任务。通过异步的 playbook 执行,Ansible 提供了灵活性,并提供了允许咱们运行咱们 playbook 特定部分的标签特性。
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
# 掌握权限提升:`become`
2+
3+
Ansible 使用了现有的权限提升系统,来以 root 权限或另一用户的权限执行任务。因为该特性允许咱们 “成为” 不同于登录到机器用户(远程用户)的另一用户,所以我们称之为 `become``become` 这个关键字,使用了现有的权限提升工具,如 `sudo``su``pfexec``doas``pbrun``dzdo``ksu``runas``machinectl` 等。
4+
5+
6+
## 使用 `become`
7+
8+
咱们可通过 `play``task` 指令、连接变量或命令行等,控制 `become` 的使用。若咱们以多种方式,设置了权限提升属性,请查看 [一般优先规则](https://docs.ansible.com/ansible/latest/reference_appendices/general_precedence.html#general-precedence-rules),以了解何种设置将被使用。
9+
10+
Ansible 中包含的所有插件完整列表,可在 [插件列表](https://docs.ansible.com/ansible/latest/plugins/become.html#become-plugin-list) 中找到。
11+
12+
13+
### `become` 指令
14+
15+
咱们可以在 play 或任务级别,设置控制 `become` 的指令。可以通过设置连接变量,咱们可覆盖这些指令,不同主机的连接变量往往不同。这些变量和指令是独立的。例如,设置 `become_user` 并不会设置 `become`
16+
17+
18+
- `become`
19+
设置为 `true` 以激活权限提升。
20+
21+
- `become_user`
22+
设置为有着所需权限的用户 - 咱们要 *成为* 的用户,而 **不是** 咱们登录的用户。这 **** 意味着在主机级别允许设置的 `become:true`,Does NOT imply `become: true`, to allow it to be set at the host level。默认值为 `root`
23+
24+
- `become_method`
25+
(于 play 或任务级别)覆盖 `ansible.cfg` 中设置的默认方式,设置为使用某种 [`become` 插件](https://docs.ansible.com/ansible/latest/plugins/become.html#become-plugins)
26+
27+
- `become_flags`
28+
(于 play 或任务级别)允许对任务或角色,使用特定开关。一种常见的用法是,当 shell 被设置为 `nologin` 时,将用户更改为 `nobody`。是在 Ansible 2.2 中添加的。
29+
30+
31+
例如,以非 root 用户身份连接时,要管理某项系统服务(需要 `root` 权限),就可使用 `become_user` 的默认值(root):
32+
33+
34+
```yaml
35+
- name: Ensure the nginx service is running
36+
service:
37+
name: nginx
38+
state: started
39+
become: true
40+
```
41+
42+
`apache` 用户身份运行一条命令:
43+
44+
45+
```yaml
46+
- name: Run a command as the apache user
47+
command: somecommand
48+
become: true
49+
become_user: apache
50+
```
51+
52+

0 commit comments

Comments
 (0)