# selecting file based on hostname

**URL:** <https://forum.ansible.com/t/selecting-file-based-on-hostname/33366>\
**Category:** Ansible Project\
**Created:** [July 10, 2020, 9:54am UTC](https://forum.ansible.com/t/selecting-file-based-on-hostname/33366 "2020-07-10T09:54:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krushna\_Chandra\_Sahu](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@Krushna\_Chandra\_Sahu](https://forum.ansible.com/u/Krushna_Chandra_Sahu)\
**Post date:** [July 10, 2020, 9:54am UTC](https://forum.ansible.com/t/selecting-file-based-on-hostname/33366/1 "2020-07-10T09:54:28Z")

</div>

Hi Team,

I need a playbook to update the sudoer file based on os distribution. I have written below code but it is failing.

- name: Copy sudoers files  
copy:  
src: files/{{ file\_name }}  
dest: /etc/sudoers  
validate: /usr/sbin/visudo -csf %s  
{% if ansible\_distribution == “amazon” %}  
file\_name: ‘sudoers.aspera’  
{% elif ansible\_distribution == “Debian” %}  
file\_name: ‘sudoers.debian’  
{% else %}  
file\_name: ‘sudoers.general’  
{% end %}

The offending line appears to be:

validate: /usr/sbin/visudo -csf %s  
{% if ansible\_distribution == “amazon” %}  
^ here

Regards

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 10, 2020, 10:43am UTC](https://forum.ansible.com/t/selecting-file-based-on-hostname/33366/2 "2020-07-10T10:43:48Z")

</div>

Do as the following, which will make playbook readable

file\_name: sudoers.general  
tasks:

- name: Set file name when Amazon  
set\_fact:  
file\_name: “sudoers.aspera”  
when: ansible\_distribution == ‘amazon’

- name: Set file name when Debian  
set\_fact:  
file\_name: “sudoers.debian”  
when: ansible\_distribution == ‘Debian’

- debug:  
msg: “{{ file\_name }}”

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 10, 2020, 10:49am UTC](https://forum.ansible.com/t/selecting-file-based-on-hostname/33366/3 "2020-07-10T10:49:17Z")

</div>

This is also possible

vars:  
file\_name\_mapping:  
amazon: “sudoers.general”  
debian: “sudoers.debian”  
tasks:

- debug:  
msg: “{{ file\_name\_mapping[ansible\_distribution] | default(‘sudoers.general’)}}”
