更新 playbook.yml,移除对 ~/.bash_aliases 的存在性检查,优化 dps 别名的添加逻辑

This commit is contained in:
严浩
2024-10-29 10:59:18 +08:00
parent e57a9adce3
commit 25afb47edc

View File

@ -12,19 +12,15 @@
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: 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:
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}}'\""
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)
create: true
# when: alias_check.rc != 0 # 只有在未找到时执行 (checks if the alias is not already defined)