lefilament / ansible

Ansible playbook
GNU Affero General Public License v3.0
5 stars 4 forks source link

docker_server_step2 could use local facts on host #4

Closed nqb closed 3 years ago

nqb commented 3 years ago

Hello,

When running docker_server_step2 roles on a host, you should have:

  1. host_vars directory created in directory where you run ansible-playbook command
  2. host_vars/name_of_your_host created

On my side, I used a directory inventory to store group_vars and host_vars inside it with an ansible.cfg file.

Regarding 1, I fixed it like this:

diff --git a/roles/docker_server_step2/tasks/main.yml b/roles/docker_server_step2/tasks/main.yml
index 609d580..dd4c663 100644
--- a/roles/docker_server_step2/tasks/main.yml
+++ b/roles/docker_server_step2/tasks/main.yml
@@ -13,7 +13,7 @@
 - name: Save uid to hostvars for dockremap user
   local_action:
     module: lineinfile
-    name: "host_vars/{{ inventory_hostname }}"
+    name: '{{ inventory_dir }}/host_vars/{{ inventory_hostname }}'
     regexp: "dockremap_subuid:"
     line: "dockremap_subuid: {{ dockremap_subuid_output.stdout.split(':')[1] }}"
   connection: local
@@ -23,7 +23,7 @@
 - name: Save gid to hostvars for dockremap user
   local_action:
     module: lineinfile
-    name: "host_vars/{{ inventory_hostname }}"
+    name: '{{ inventory_dir }}/host_vars/{{ inventory_hostname }}'
     regexp: "dockremap_subgid:"
     line: "dockremap_subgid: {{ dockremap_subgid_output.stdout.split(':')[1] }}"
   connection: local

I'm thinking about 2. IMHO, it will be more simple to store such variables directly on host in /etc/ansible/facts.d as a local facts.

remi-filament commented 3 years ago

Thank you for your issue @nqb This is indeed the way we use Ansible, by creating each new server in host_vars (we copy the variables from Template_xx files into a new file per server), therefore it makes sense to store all host related variables in this file. I have added your suggestion for point 1 here : 2e8910490ee1f61350f75d25d3bb9818502b33f1

I understand that your solution would allow for people having inventory variables defined elsewhere, however this would split variables in 2 locations therefore I am not really in favor... What I did is that I added a create: yes argument so that if file does not exist it would create it (you would then have only these variables in this host_vars file instead of in a facts.d directory) here : bf764163c44776118d2590ae6df024d5dd4b9bc8

Let me know your thoughts ?

nqb commented 3 years ago

I have added your suggestion for point 1 here : 2e89104

Thanks!

Solution for 2. looks good to me.

I understand that your solution would allow for people having inventory variables defined elsewhere, however this would split variables in 2 locations therefore I am not really in favor...

In fact, it's possible to install a script in /etc/ansbile/facts.d run by Ansible each time you gather facts. That way, you don't have to really store variables, Ansible will collect them.

IMHO, it's better to let Ansible detect host vars information directly on nodes in place of redefining a lot of informations inside inventory.

remi-filament commented 3 years ago

I guess both point of views are valid :) I have frequent issues with facts when using --start-at-task parameter since it would not collect facts in that case.

If you do not mind, I would keep my approach and close this issue. If you do mind, let me know.

nqb commented 3 years ago

Thanks for your feedback @remi-filament.

It's ok for me.