Skip to content

Commit

Permalink
Ensure idempotency when checking for shell rc (#5)
Browse files Browse the repository at this point in the history
This change makes the role properly idempotent by removing a call to the
`ansible.builtin.file` module and using the capabilities of the
`ansible.builtin.blockinfile` module to create the destination file if
it does not already exist.
  • Loading branch information
DWSR authored Nov 4, 2020
1 parent e321877 commit 0f4c018
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.1.1] - 2020-11-03

### Fixes

* Pass `create: true` when amending the shell rc file instead of relying on a separate `file` module
call to ensure that the file exists. This makes the role properly idempotent and slightly simpler.

## [0.1.0] - 2020-11-03

* Initial commit
1 change: 1 addition & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ provisioner:
hosts: test_kitchen
role_name: profile_d
ansible_sudo: false
idempotency_test: true
# Don't install via Ansible omnibus
require_ansible_omnibus: false
# Don't install via OS package manager. This is broken on Focal
Expand Down
37 changes: 14 additions & 23 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@

- name: Configure ZSH
block:
- name: Ensure ~/.zshrc exists
ansible.builtin.file:
path: "{{ ansible_user_dir }}/.zshrc"
state: touch
- name: Add bashcompinit for ZSH
ansible.builtin.copy:
backup: false
Expand All @@ -38,6 +34,7 @@
- name: Amend ~/.zshrc to load ~/.profile.d
ansible.builtin.blockinfile:
backup: true
create: true
block: |
# Source profile.d files
for f in $(ls "$HOME/.profile.d/"); do
Expand All @@ -49,23 +46,17 @@
path: "{{ ansible_user_dir }}/.zshrc"
when: '"zsh" in profile_d_shell'


- name: Configure Bash
block:
- name: Ensure ~/.bashrc exists
ansible.builtin.file:
path: "{{ ansible_user_dir }}/.bashrc"
state: touch
- name: Amend ~/.bashrc to load ~/.profile.d
ansible.builtin.blockinfile:
backup: true
block: |
# Source profile.d files
for f in $(ls "$HOME/.profile.d/"); do
# Use ls instead of find to avoid relying on external binaries
source "$HOME/.profile.d/$f"
done
insertafter: EOF
marker: "# {mark} PROFILE.D BLOCK"
path: "{{ ansible_user_dir }}/.bashrc"
- name: Amend ~/.bashrc to load ~/.profile.d
ansible.builtin.blockinfile:
backup: true
create: true
block: |
# Source profile.d files
for f in $(ls "$HOME/.profile.d/"); do
# Use ls instead of find to avoid relying on external binaries
source "$HOME/.profile.d/$f"
done
insertafter: EOF
marker: "# {mark} PROFILE.D BLOCK"
path: "{{ ansible_user_dir }}/.bashrc"
when: '"bash" in profile_d_shell'

0 comments on commit 0f4c018

Please sign in to comment.