Files
ansible-example/playbook.yml

26 lines
2.1 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--- # ansible-playbook -i hosts.ini playbook.yml -v # -vv
- hosts: debian_servers
become: true # 的作用是提升任务的执行权限。具体来说它允许任务在目标主机上以不同于当前用户的身份运行通常是以超级用户root的身份。这在需要执行需要更高权限的操作时非常有用例如安装软件包、修改系统配置文件等。
vars: # Python 解释器警告: 输出中有关于 Python 解释器的警告。这并不影响 Ansible 的执行,但提醒你未来可能会因安装不同版本的 Python 而导致路径变更。若需消除警告,可以显式指定使用的 Python 解释器。在 Playbook 中添加如下变量:
ansible_python_interpreter: /usr/bin/python3
tasks:
- name: Update and upgrade all packages
ansible.builtin.apt: # 这是 Ansible 的一个模块,用于管理 APT 包管理系统。(https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html)
update_cache: true # 这会更新 APT 包管理系统的缓存,相当于运行 sudo apt-get update。
upgrade: dist # 这会执行一个发行版升级,相当于运行 sudo apt-get dist-upgrade。
cache_valid_time: 3600 # 缓存有效时间为 1 小时
autoremove: true # 这会删除不再需要的软件包,相当于运行 sudo apt-get autoremove。
autoclean: true # 这会删除所有已下载的软件包,但仍保留软件包的索引文件,相当于运行 sudo apt-get autoclean。
# - name: Check if alias dps is defined
# shell: grep 'alias dps=' ~/.bash_aliases
# register: alias_check
# ignore_errors: true # 忽略错误,以便在未找到时继续执行
- name: Add alias dps to ~/.bash_aliases if not defined
lineinfile: # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html
path: ~/.bash_aliases
line: "alias dps=\"docker ps --format 'table {{ '{{' }}.ID{{ '}}' }}\\t{{ '{{' }}.Names{{ '}}' }}'\""
state: present
create: true
# when: alias_check.rc != 0 # 只有在未找到时执行 (checks if the alias is not already defined)