Aliases on guest machine with Ansible
Asked Answered
E

2

6

I want to add some aliases on a guest machine running through Vagrant. My provisioner is Ansible, and it would be great if I can add the aliases with a task on the playbook, I would prefer not having to modify my Vagrantfile.

I have tried both these in a role:
command: alias serve='pub serve --hostname=0.0.0.0'
shell: "alias serve='pub serve --hostname=0.0.0.0'"
but none of them has worked. The first raises an exception which I think comes from the ''. The second one simply doesn't add the alias.

Endothermic answered 10/5, 2015 at 5:31 Comment(0)
B
20

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.

Benford answered 10/5, 2015 at 9:47 Comment(5)
Thanks for your answer, i'll try it out. I did something similar, copying a .bash_aliases file from my host machine into the guest machine on the provisioning, with - 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
And nice explanation btw about why did the command didn't work, I couldn't figure it out.Endothermic
In order to accept your answer you should modify the code a little bit. There is a problem with the line key because of the ''. I used a variable defined in defaults, and then did - name: Add serve alias for vagrant user lineinfile: dest=/home/vagrant/.bash_aliases line="{{pub_server_alias}}" regexp='^'"{{pub_server_alias}}"'$' state=present insertafter=EOF create=TrueEndothermic
I generally find that blockinfile is a lot easier. Especially when it comes to changing the alias.Raynaraynah
@Raynaraynah This answer predates blockinfile but you should either add an edit to this answer with that option or a separate answer if you think it adds value.Benford
A
2

Thanks to this answer by , After you add alias line by:

- name: Add serve alias for foo user
  lineinfile:
    path=/home/foo/.bashrc
    line="alias serve='pub serve --hostname=0.0.0.0'"
    regexp='^alias serve='pub serve --hostname=0.0.0.0'$'
    state=present
    insertafter=EOF
    create=True

You need to source the file also:

- name: Source .bashrc
  shell: "source /home/foo/.bashrc"
  args:
    executable: /bin/bash
Ada answered 17/5, 2022 at 9:58 Comment(2)
This answer was really helped me and BTW - the owner is not mandatory.Rodger
Ok, I ll remove the ownerBouchard

© 2022 - 2024 — McMap. All rights reserved.