From e57a9adce3492faf866fcf2c7fc26871b20e4a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=A9?= Date: Tue, 29 Oct 2024 10:40:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20playbook.yml=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20APT=20=E6=A8=A1=E5=9D=97=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E6=9C=89=E6=95=88?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=92=8C=E8=87=AA=E5=8A=A8=E6=B8=85=E7=90=86?= =?UTF-8?q?=E9=80=89=E9=A1=B9=EF=BC=9B=E6=96=B0=E5=A2=9E=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=20~/.bash=5Faliases=20=E5=AD=98=E5=9C=A8=E5=8F=8A=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=AB=E5=90=8D=20dps=20=E7=9A=84=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playbook.yml | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/playbook.yml b/playbook.yml index 3cc6c9e..1bcb49b 100644 --- a/playbook.yml +++ b/playbook.yml @@ -1,13 +1,30 @@ ---- # ansible-playbook -i hosts.ini playbook.yml -vv +--- # 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 包管理系统。 - update_cache: yes # 这会更新 APT 包管理系统的缓存,相当于运行 sudo apt-get update。 + 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: yes # 这会删除不再需要的软件包,相当于运行 sudo apt-get autoremove。 - autoclean: yes # 这会删除所有已下载的软件包,但仍保留软件包的索引文件,相当于运行 sudo apt-get autoclean。 + autoremove: true # 这会删除不再需要的软件包,相当于运行 sudo apt-get autoremove。 + autoclean: true # 这会删除所有已下载的软件包,但仍保留软件包的索引文件,相当于运行 sudo apt-get autoclean。 + + - name: Ensure ~/.bash_aliases exists + file: + path: ~/.bash_aliases + state: touch + + - 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: + path: ~/.bash_aliases + line: "alias dps=\"docker ps --format 'table {{.ID}}\\t{{.Names}}'\"" + state: present + when: alias_check.rc != 0 # 只有在未找到时执行 (checks if the alias is not already defined) \ No newline at end of file