The shell option looks right but it will only create the alias for the session. Then when you connect to it after the provisioning then it won't exist. I'm also unsure if further plays in the playbook will be able to use it as they may use another SSH session.
To add it permanently you will need to add it to the relevant .bashrc
or .bash_aliases
(which is then sourced by .bashrc
) depending on your Linux flavour/version.
To do this you could simply use:
shell: echo "alias serve='pub serve --hostname=0.0.0.0'" >> ~/.bash_aliases
But this would not be idempotent and rerunning the Ansible playbook or the Vagrant provisioning (which would rerun the Ansible playbook) would add duplicate lines each time.
Instead you should use the lineinfile module with something like:
- name: Add serve alias for foo user
lineinfile:
path=/home/foo/.bashrc
line="alias serve='pub serve --hostname=0.0.0.0'"
owner=foo
regexp='^alias serve='pub serve --hostname=0.0.0.0'$'
state=present
insertafter=EOF
create=True
The above play will add the alias line to the end of the foo user's .bashrc
file (creating the file if it doesn't exist for some reason). When you rerun it, it attempts to match the regexp value (this might need tweaking with escaping parts) and if it matches it then it will replace it with the line value.
- name: Copying bash aliases copy: src=../tasks/.bash_aliases dest=/home/vagrant/.bash_aliases
and it worked, but it will replace the file each time I provision, and if I ever have aliases for different roles this could be a problem. Your solution seems cleaner. – Endothermic