How to delete host entries from hosts file using Ansible

How to delete host entries from hosts file using Ansible

In Ansible we run an ansible play against a target hosts group, the ansible play will be executed on each remote host that is part of that particular target group.

Lets say we want to pass the host details dynamically to host file and every time we run a play we want to run them on new host passed dynamically.

In order to achieve this there are multiple ways lets discuss the one way of doing it.

we should remove the host's entries from /etc/ansible/hosts file each time before running any ansible play. Below ansible-play which will remove the host entries and replace them with a blank line

--
- hosts: localhost
? become: true
? tasks:
? - name: loop debug inventory hostnames
? ? debug:
? ? ? msg: "{{ item }}"
? ? with_inventory_hostnames: ameyo
? ? register: hostip
? - name: remove host ip's
? ? lineinfile:
? ? ? path: /etc/ansible/hosts
? ? ? regexp: "{{item}}"
? ? ? line: ''
? ? with_inventory_hostnames: <target_grouo_name>        

要查看或添加评论,请登录

Mahira Technology Pvt Ltd的更多文章