i'm using ansible automate vm configuration of our build environments, e.g. setting path
, proxy environment variables, installing global node.js tools, etc. works great, , it's easy automate common setup tasks on multiple hosts. of tasks split on ansible roles
separate responsibilities , additional reusability.
now i've run following: new package installed, need change path
environment variable - no longer needs entry added part of ansible setup.
so naturally went ahead , updated ansible task use set path variable new environments , removed custom entry (i used lineinfile
task create path entry).
lineinfile: dest: ~/.profile regexp: "^export {{ item.regexp }}" line: "export {{ item.line }}" create: yes with_items: - { regexp: 'path', line: 'path=:~/bin:~/node_modules/.bin:$path' }
i removed 1 entry above line
part of item. can see, lineinfile
task checks entry export path
present in file, , if isn't, creates export path=:~/bin:~/node_modules/.bin:$path
.
now question: since lineinfile task verifies entry exists, doesn't check complete value, need make sure of existing environments updated. how go this?
is there way make lineinfile
statement smart enough verify content of line well, i.e. ensuring exact line want present (at same time avoiding duplicate path
entries)? or complex?
the other thing thought (and did) create one-off playbook updates path
variable in environments:
lineinfile: dest: ~/.profile backrefs: yes regexp: "^export path=(.*)/opt/foo/bar/baz/bin[\\:]*(.*)" line: "export path=\\1\\2"
is common scenario - creating playbooks one-off tasks? benefit playbook run on hosts , avoids manual work. reusability outside of minimal, expect once.
is right way go this, or there better, smarter way doing tasks this?
which version of ansible using? since describing looks bug, because lineinfile
module should detect line changed , update it. copy-pasted first task (just changed path file) , works should.
here first run:
➜ ~ % ansible-playbook test.yml play [all] ******************************************************************** task: [lineinfile ] *********************************************************** changed: [localhost] => (item={'regexp': 'path', 'line': 'path=:~/bin:~/node_modules/.bin:$path'}) play recap ******************************************************************** localhost : ok=1 changed=1 unreachable=0 failed=0
than removed ~/bin
path , ran whole thing again:
➜ ~ % ansible-playbook test.yml play [all] ******************************************************************** task: [lineinfile ] *********************************************************** changed: [localhost] => (item={'regexp': 'path', 'line': 'path=:~/node_modules/.bin:$path'}) play recap ******************************************************************** localhost : ok=1 changed=1 unreachable=0 failed=0
as can see module detected changed variable , updated file. works should.