Asible, how to add a user to a group only if the user exists

I am writing a book to set up a development environment and build a pipeline for a project. I would like to share the playbook with the real infrastructure as much as possible and just rely on groups, so I could configure the pipeline inside the local virtual machine, as well as on bare hardware with the same team, just using different group delimiters.

Now the problem is that with local Vagrant boxes I need to add a user firewall to the docker group, where, like bare metal servers, there is some kind of arbitrary user. What is the best practice for handling such variations in a playbook while keeping it as abstract as possible? Should I use behavioral parameters, host variables, group variables, conditional expressions or facts for this kind of thing?

Is it possible, or reasonable, to maintain a fully abstract notebook without any hard-coded values ​​and adaptability to any environment?

+5
source share
1 answer

To answer your specific question:

Use a user variable rewritten by Vagrantfile. For example, it will be ubuntu if in aws or vagrant if you are in a vagrant state.


A more theoretical answer:

I had the same problem with stray assemblies, and I decided to leave the toy somewhere available so that all stray assemblies would pick it up and run it to configure the server.

For example, a playbook looks like this:

  - hosts: all tasks: - name: remove apparmor apt: name=apparmor purge=true state=absent 

and then each of my stray repositories launched it and launched using

 box.vm.provision :ansible do |ansible| ansible.playbook = 'vagrant_extra.yml' ansible.sudo = true ansible.limit = 'all' end 

The best solution, I think, is to use the packer to create your own stray image, which has all the things that are supported there, so that

  • You do not need to spend time creating an image yourself, and
  • your fields match the prod fields.
+1
source

Source: https://habr.com/ru/post/1259265/


All Articles