Ansible Regex Not Matching SSH Configuration Block, Is There A Better Way?

Backstory:

I’m making an Ansible role that creates an SSH key for hosts that need one and automatically updates my configuration file with the latest information; lan ip, username, key, etc. I can’t get the Ansible to delete the existing block correctly though before adding the new one. Then when it adds the new one, it overwrites the other hosts block for some reason (so at the end, only 1 block exists even if there are 10 hosts. I’ll fix the latter later, but my main concern is to get the old block deleted correctly.


Question(s) & tl;dr:

Is there a better approach to doing this, does anyone see why this regex doesn’t work in Ansible, and could my regex be simplified/improved?


Here’s the relevant tasks from my Ansible role

- name: Read existing SSH config
  slurp:
    src: "{{ ssh_config_dir }}/config"
  register: ssh_config_file

- name: Decode existing SSH config
  set_fact:
    ssh_config_content: "{{ ssh_config_file.content | b64decode }}"

- name: Parse existing SSH config into lines
  set_fact:
    ssh_config_lines: "{{ ssh_config_content.split('n') }}"

- name: Check if existing host entry matches
  set_fact:
    host_entry_valid: >
      {{ ssh_config_lines | select('match', '^Host {{ inventory_hostname }}$') | list | length > 0 and
         ssh_config_lines | select('match', '^\s*Hostname {{ hostvars[inventory_hostname].ansible_host }}$') | list | length > 0 and
         ssh_config_lines | select('match', '^\s*User {{ ssh_remote_user }}$') | list | length > 0 and
         ssh_config_lines | select('match', '^\s*Port {{ ssh_port }}$') | list | length > 0 and
         ssh_config_lines | select('match', '^\s*IdentityFile {{ ssh_key_dir }}/{{ inventory_hostname }}{{ ssh_key_name_suffix }}$') | list | length > 0 }}

- name: Debug host entry validity
  debug:
    var: host_entry_valid

- name: Backup the existing SSH config
  copy:
    src: "{{ ssh_config_dir }}/config"
    dest: "{{ ssh_config_dir }}/config.bak"
  when: not host_entry_valid

- name: Define the regex pattern
  set_fact:
    my_regex: '^(s+)?Hosts+{{ inventory_hostname }}(s+)?$n^(((s+)?[A-Za-z0-9./_-]|#)+(s+)?)$n^(s+)?$'

- name: Print regex pattern
  debug:
    msg: "{{ my_regex | quote }}"

- name: Remove existing host entry if it doesn't match
  lineinfile:
    path: "{{ ssh_config_dir }}/config"
    state: absent
    regexp: '^(s+)?Hosts+{{ inventory_hostname }}(s+)?$n^(((s+)?[A-Za-z0-9./_-]|#)+(s+)?)$n^(s+)?$'
  when: not host_entry_valid

Check if existing host entry matches needs works, but it should trigger the Remove existing host entry if it doesn't match task which I’m having problems with. The regex matches a block perfectly in VSCode, but in Sublime it matches multiple blocks and Ansible it doesn’t seem to work at all.


This started out as a ChatGPT creation, but I tweaked some of it and ended up writing the regex myself.

This works in VSCode:

^(s+)?Host test(s+)?$n^(((s+)?[A-Za-z0-9./_-]|#)+(s+)?)$n

but the negative(?) look ahead does not. It does work in Sublime, but sublime matches multiple blocks for some reason.

^(s+)?Host test(s+)?$n^(((s+)?[A-Za-z0-9./_-]|#)+(s+)?)$n(?=Host)

The idea is to match Host hostname and then look for any lines that contain text until there is a blank line; and with the negative look ahead check to see if host exists after the blank line, but I’m not familiar with using lookaheads and this won’t work if the block is the last block in the file, so I’ll be removing that.


Here’s the relevant Ansible output and the old SSH block doesn’t get removed:

TASK [ssh-keys : Remove existing SSH agents and known hosts] ********************************************************************************************
included: /scripts/ansible/roles/ssh-keys/tasks/remove_existing_ssh_agents.yml for test

TASK [ssh-keys : Remove all SSH agents] *****************************************************************************************************************
skipping: [test]

TASK [ssh-keys : Remove host from known hosts] **********************************************************************************************************
skipping: [test]

TASK [ssh-keys : Copy SSH key to remote server] *********************************************************************************************************
included: /scripts/ansible/roles/ssh-keys/tasks/copy_key.yml for test

TASK [ssh-keys : Copy SSH key to remote server] *********************************************************************************************************
skipping: [test]

Example block:

Host test
   HostName 10.0.0.4
   User myuser
   IdentityFile ...

Host test2
  ...

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật