Starting out with ansible and trying to configure sshd config. Here is what I have, it works but is there a better way to do with for future reference. e.g. if I have many different distributions it looks like a lot of copying. Can I combine the 2 tasks?
You can create a variable that contains the information
allowusers:
Amazon: ec2-user
Centos: centos
And then you only need one task
- name: sshd_config
lineinfile:
path: /etc/ssh/sshd_config
regexp: "^AllowUsers"
line: AllowUsers {{ allowusers[ansible_distribution] }}
notify: restart sshd
You probably looking for ansible_os_family which is RedHat for these distributions : RedHat EL, CentOS, Amazon, etc…
You should probably test the major version (between 6 and 7, there’s service and systemctl which are kind different for example, I suppose you’re not modifying only ssh config)
So you’re when directive should be something like this :
when: ansible_os_family == ‘RedHat’ and ansible_distribution_major_version in [‘6’,‘7’]