Ansible: Add a stanza to .ssh/config without overwriting the file

Shadur

I'm working on an restic and SSH-based backup solution implemented via Ansible. Omitting the details, it uses sftp:backups-{{ restic_backup_name }}:{{ inventory_hostname }} as the repository URL, which means that I need to add the following stanza to .ssh/config to the backup user on the sending server:

Host backup-{{ restic_backup_name }}
    HostName {{ restic_backup_host }}
    User restic-backup
    IdentityFile /etc/restic/{{ restic_backup_name }}.key

As you can see, there's no problem generating the stanza from a template, but in the (probably rare, but I'm trying to account for edge cases) case where a .ssh/config already exists I don't want to overwrite the existing file, just add this stanza to it.

(Skipping this step if it exists already would be nice, but that's optional for now)

Shadur

While working on this question I realized that the blockinfile will do what I want:

- name: Create SSH config block
  blockinfile:
    path: /root/.ssh/config
    block: |
        Host backup-{{ restic_backup_name }}
            HostName {{ restic_backup_host }}
            User restic-backup
            IdentityFile /etc/restic/{{ restic_backup_name }}.key        
    backup: yes
    validate: /usr/sbin/sshd -T -f %s

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add new data to a text file without overwriting it in python

From Dev

Need to add a node to an XML file in VBA without overwriting older node

From Dev

How to add-content to a log file without overwriting?

From Dev

Add to file instead of overwriting

From Dev

paste into a txt file without overwriting

From Dev

Add Binding To IIS With PowerShell Without Overwriting Existing

From Dev

How to add values to Firebase Firestore without overwriting?

From Dev

How to make a file with the same name as another txt file,without overwriting the file but instead add a number to the existing one in python?

From Dev

How to append to an existing file in R without overwriting it?

From Dev

Write in an existing file without overwriting in Fortran

From Dev

How to write to a file without overwriting current contents?

From Dev

Writing to an existing file without overwriting/erasing

From Dev

Batch File Copy and Move without overwriting

From Dev

How to append to an existing file in R without overwriting it?

From Dev

How to write to a file without overwriting current contents?

From Dev

vbscript replacing text without overwriting in a text file

From Dev

Linux cmd to copy file to an existing file without overwriting it

From Dev

How do I copy a file without overwriting an existing file?

From Dev

Add associations from mixin without overwriting existing associations

From Dev

Add ErrorTemplate to a MahApp control without overwriting its default style

From Dev

Add post build event without overwriting existing events

From Dev

How to add to an array without overwriting (from read line and explode) PHP

From Dev

How to add new items to DataGrid without overwriting the old ones?

From Dev

Add ErrorTemplate to a MahApp control without overwriting its default style

From Dev

How to add to an array without overwriting (from read line and explode) PHP

From Dev

Firebase add another data to a child without overwriting existing data

From Dev

Replacing a string within a stream in C# (without overwriting the original file)

From Java

How to write to an existing excel file without overwriting data (using pandas)?

From Dev

c++ boost library - writing to ini file without overwriting?

Related Related

  1. 1

    Add new data to a text file without overwriting it in python

  2. 2

    Need to add a node to an XML file in VBA without overwriting older node

  3. 3

    How to add-content to a log file without overwriting?

  4. 4

    Add to file instead of overwriting

  5. 5

    paste into a txt file without overwriting

  6. 6

    Add Binding To IIS With PowerShell Without Overwriting Existing

  7. 7

    How to add values to Firebase Firestore without overwriting?

  8. 8

    How to make a file with the same name as another txt file,without overwriting the file but instead add a number to the existing one in python?

  9. 9

    How to append to an existing file in R without overwriting it?

  10. 10

    Write in an existing file without overwriting in Fortran

  11. 11

    How to write to a file without overwriting current contents?

  12. 12

    Writing to an existing file without overwriting/erasing

  13. 13

    Batch File Copy and Move without overwriting

  14. 14

    How to append to an existing file in R without overwriting it?

  15. 15

    How to write to a file without overwriting current contents?

  16. 16

    vbscript replacing text without overwriting in a text file

  17. 17

    Linux cmd to copy file to an existing file without overwriting it

  18. 18

    How do I copy a file without overwriting an existing file?

  19. 19

    Add associations from mixin without overwriting existing associations

  20. 20

    Add ErrorTemplate to a MahApp control without overwriting its default style

  21. 21

    Add post build event without overwriting existing events

  22. 22

    How to add to an array without overwriting (from read line and explode) PHP

  23. 23

    How to add new items to DataGrid without overwriting the old ones?

  24. 24

    Add ErrorTemplate to a MahApp control without overwriting its default style

  25. 25

    How to add to an array without overwriting (from read line and explode) PHP

  26. 26

    Firebase add another data to a child without overwriting existing data

  27. 27

    Replacing a string within a stream in C# (without overwriting the original file)

  28. 28

    How to write to an existing excel file without overwriting data (using pandas)?

  29. 29

    c++ boost library - writing to ini file without overwriting?

HotTag

Archive