介绍
在本实验中,你将学习如何使用 Ansible 的 Shell 模块在远程主机上执行 shell 命令。当现有 Ansible 模块无法覆盖你需要运行的 shell 命令,或者你需要对执行过程有更高的灵活性和控制时,Shell 模块非常有用。
在本实验中,你将学习如何使用 Ansible 的 Shell 模块在远程主机上执行 shell 命令。当现有 Ansible 模块无法覆盖你需要运行的 shell 命令,或者你需要对执行过程有更高的灵活性和控制时,Shell 模块非常有用。
在这一步中,你将使用 Ansible 的 Shell 模块在远程主机上执行一个简单的 shell 命令。
首先,完成 /home/labex/project/simple_shell_command.yml
文件。
在 playbook 文件中添加以下内容:
- name: Execute a Simple Shell Command
hosts: localhost
gather_facts: false
tasks:
- name: Execute ls -l command
shell: ls -l
register: command_output
- name: Display command output
debug:
var: command_output.stdout_lines
hosts: localhost
:执行此 Ansible playbook 的目标主机是 localhost
。gather_facts: false
:禁用主机信息的收集。shell: ls -l
:指定要执行的命令。总结来说,此 playbook 的目的是在本地主机上执行 ls -l
命令并显示命令的输出。
然后,在 Ansible playbook 中显示命令的输出。
ansible-playbook /home/labex/project/simple_shell_command.yml
示例输出:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Execute a Simple Shell Command] ******************************************
TASK [Execute ls -l command] ***************************************************
changed: [localhost]
TASK [Display command output] **************************************************
ok: [localhost] => {
"command_output.stdout_lines": [
"total 16",
"-rwxrwxrwx 1 labex root 285 Mar 8 13:33 complex_shell_commands.yml",
"-rwxrwxrwx 1 labex root 221 Mar 8 13:33 handle_command_failure.yml",
"-rwxrwxrwx 1 labex root 222 Mar 8 13:33 pass_variables.yml",
"-rwxrwxrwx 1 labex root 266 Mar 8 13:36 simple_shell_command.yml"
]
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
这里的 "command_output.stdout_lines":[...]
是目标主机上 ls -l
命令的输出。
在这一步中,你将学习如何从 Ansible playbook 向使用 Shell 模块执行的 shell 命令传递变量。
首先,完成 /home/labex/project/pass_variables.yml
文件。
在 playbook 文件中添加以下内容:
- name: Pass Variables to Shell Commands
hosts: localhost
gather_facts: false
vars:
directory_path: /home/labex
tasks:
- name: Execute ls command with a variable
shell: ls "{{ directory_path }}"
register: variable_command_output
- name: Display variable command output
debug:
var: variable_command_output.stdout_lines
hosts: localhost
:执行此 Ansible playbook 的目标主机是 localhost
。vars
:定义一个名为 directory_path
的变量,其值为 /home/labex
。shell: ls "{{ directory_path }}"
:执行 ls
命令以输出名为 directory_path
的变量所表示的目录内容。总结来说,此 playbook 在本地主机上使用表示目录路径的变量(/home/labex
)执行 ls
命令,并显示命令的输出。
然后,在 Ansible playbook 中显示命令的输出。
ansible-playbook /home/labex/project/pass_variables.yml
示例输出:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Pass Variables to Shell Commands] ****************************************
TASK [Execute ls command with a variable] **************************************
changed: [localhost]
TASK [Display variable command output] *****************************************
ok: [localhost] => {
"variable_command_output.stdout_lines": [
"Code",
"Desktop",
"golang",
"project"
]
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
这里的 "variable_command_output.stdout_lines": [...]
是目标主机上 ls /home/labex
命令的输出。
在这一步中,你将学习如何在使用 Shell 模块执行 shell 命令时处理命令失败的情况。你将处理 shell 命令返回非零退出状态(表示失败)的情况。
首先,完成 /home/labex/project/handle_command_failure.yml
文件。
在 playbook 文件中添加以下内容:
- name: Handle Command Failure
hosts: localhost
gather_facts: false
tasks:
- name: Execute a failing command
shell: failing-command
register: failing_command_output
ignore_errors: yes
- name: Handle command failure
debug:
msg: "The command failed. Performing fallback action."
when: failing_command_output.failed
hosts: localhost
:执行此 Ansible playbook 的目标主机是 localhost
。shell: failing-command
:这一行使用 shell 模块执行命令 failing-command
。该命令的输出将被捕获并存储在 failing_command_output
变量中。由于 failing-command
脚本不存在,此命令必定会失败。ignore_errors: yes
:这一行指示 Ansible 忽略执行 failing-command
时发生的任何错误。这使得 playbook 在命令失败后仍能继续执行。debug
:此模块使用 debug 模块显示一条消息,内容为 "The command failed. Performing fallback action."。此消息仅在 failing_command_output.failed
条件满足时显示,表示之前的命令执行失败。总结来说,此 playbook 尝试执行一个预期会失败的命令,忽略执行过程中发生的任何错误,然后根据命令的失败状态显示一条消息,表明命令失败并执行回退操作。
然后,在 Ansible playbook 中显示命令的输出。
ansible-playbook /home/labex/project/handle_command_failure.yml
示例输出:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Handle Command Failure] **************************************************
TASK [Execute a failing command] ***********************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "failing-command", "delta": "0:00:00.009169", "end": "2024-03-08 13:46:22.701946", "msg": "non-zero return code", "rc": 127, "start": "2024-03-08 13:46:22.692777", "stderr": "/bin/sh: line 1: failing-command: command not found", "stderr_lines": ["/bin/sh: line 1: failing-command: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Handle command failure] **************************************************
ok: [localhost] => {
"msg": "The command failed. Performing fallback action."
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
"msg": "The command failed. Performing fallback action."
的存在表明 failing-command
命令的执行失败。
在这一步中,你将使用 Shell 模块执行更复杂的 shell 命令。你将探索命令重定向、管道和复杂命令结构的用法。
首先,完成 /home/labex/project/complex_shell_commands.yml
文件。
在 playbook 文件中添加以下内容:
- name: Execute Complex Shell Commands
hosts: localhost
gather_facts: false
tasks:
- name: Execute complex command
shell: ls -l /home/labex | grep 'project' > /home/labex/output.txt
register: complex_command_output
- name: Display complex command output
debug:
var: complex_command_output.stdout_lines
hosts: localhost
:执行此 Ansible playbook 的目标主机是 localhost
。shell
:将 ls -l /home/labex
的输出重定向到 /home/labex/output.txt
文件,并使用 grep
过滤与 project
相关的内容。总结来说,此 playbook 在本地主机上执行一个复杂的 shell 命令,过滤并将输出重定向到文件,然后显示命令执行的输出。
然后,执行 Ansible playbook 命令。
ansible-playbook /home/labex/project/complex_shell_commands.yml
示例输出:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Execute Complex Shell Commands] ******************************************
TASK [Execute complex command] *************************************************
changed: [localhost]
TASK [Display complex command output] ******************************************
ok: [localhost] => {
"complex_command_output.stdout_lines": []
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
最后,查看 /home/labex/output.txt
文件内容:
cat /home/labex/output.txt
示例输出:
drwxr-xr-x 1 labex labex 4096 Mar 8 13:49 project
恭喜!你已经成功完成了 Ansible Shell 模块的实验。你学会了如何使用 Shell 模块在远程主机上执行 shell 命令、向命令传递变量、处理命令失败以及执行复杂的 shell 命令。
Shell 模块为你在 Ansible playbook 中执行 shell 命令提供了极大的灵活性和控制力。然而,在使用时需要谨慎,因为运行任意的 shell 命令可能会带来安全隐患。请始终确保你信任这些命令,并对任何用户提供的输入进行清理。
现在你已经对 Shell 模块有了很好的理解,可以利用它来执行各种自动化任务并高效管理你的远程主机。祝你编写 Ansible 脚本愉快!