1

In ansible i created a sshd_config which will be copied on servers which are part of sit group

[sit]
192.168.18.10
192.168.18.11
192.168.18.12
192.168.18.13
192.168.18.14
192.168.18.15

- name: Deploy SSHD_Configuration                                                    
  tags: new                                                                          
  template:                                                                          
    src: "~/ansible_files/roles/common/base/templates/{{group_names}}/sshd_config.j2"
    dest: "/etc/ssh/sshd_config"                                                     
    owner: root                                                                      
    group: root                                                                      
    mode : 0600                                                                      
  notify:                                                                            
    - "restart sshd service" 

But when i run it, it gives this error

fatal: [192.168.18.10]: FAILED! => {"changed": false, "msg": "Could not find or access '~/ansible_files/roles/common/base/templates/[u'sit']/sshd_config.j2' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

Now we have multiple environments and each environment has different sshd_configuration file which need to be deployed , My idea was to use the special hosts variable but couldn't find a way to use it, So in ansible documentation i came across group_names which checks against the current host but this variable is expanding to [u'sit']

Is there a way to overcome this or even better way?

Thanks

3
  • The error message is: "Could not find or access '~/ansible_files/roles/common/base/templates/[u'sit']/sshd_config.j2' Did you try to use the absolute path without ~ , i.e: /home/someuser/ansible_files/....... instead? Tilde (~) does not expand when quoted but not sure if this is the cause Nov 10, 2021 at 11:47
  • ~ do expand, but issue is group_names variable expands to "[u'sit']". I saw demos many used group_names without any issue
    – OmiPenguin
    Nov 10, 2021 at 12:16
  • Well ansible has expanduser to expand paths containing a tilde (not sure from which version).. but if you tested this and you think the path isn;'t the problem, then I think you are missing with_items: "{{group_names}}" just under the mode: 0600 and it should be at the same level as template: Nov 10, 2021 at 13:24

1 Answer 1

0

Since the comments don't format well, I'm going to post this as an aswer:

Give the data you provided:

[sit]
192.168.18.10
192.168.18.11
192.168.18.12
192.168.18.13
192.168.18.14
192.168.18.15

- name: Deploy SSHD_Configuration                                                    
  tags: new                                                                          
  template:                                                                          
    src: "~/ansible_files/roles/common/base/templates/{{item}}/sshd_config.j2"
    dest: "/etc/ssh/sshd_config"                                                     
    owner: root                                                                      
    group: root                                                                      
    mode : 0600 
  with_items: "{{group_names}}"                                                                     
  notify:                                                                            
    - "restart sshd service" 

This will work for multiple lists. If you have a single list like [sit] you could also use group_names[0] referring to the first element

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .