Ansible 正则表达式从 /etc/hosts 中删除匹配的 IP
Ansible regex to delete matching IPs from /etc/hosts
我正在尝试更改远程节点的主机名并且此部分有效:
- name: Change the hostname
lineinfile: dest=/etc/hosts
regexp='.*{{ item }}$'
line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
因此,它会将 IP hostname FQDN
附加到 /etc/hosts
文件的末尾。
我想要实现的是删除现有条目然后添加此部分,这是我尝试过的:
- name: Change the hostname
lineinfile: dest=/etc/hosts
# regexp='.*{{ item }}$'
regexp='{{ hostvars[item].ansible_default_ipv4.address }}'
state=absent
# line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
# state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
但是,这一直显示以下错误:
The offending line appears to be:
# regexp='.*{{ item }}$'
regexp="{{ hostvars[item].ansible_default_ipv4.address }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.
将引号从 ''
更改为 ""
似乎不起作用。我的问题是:
- 这样查询IP是否正确,以便删除
主机文件中的条目?
- 如果是这样,有人可以指出错误
句法?
- 有没有更高效的方法来实现去除任务
(所有)现有条目,然后在
/etc/hosts
? 中添加行
您不能使用 Ansible 表示法(带等号)并将其视为 YAML。
您的代码的问题不在于引用,而在于您在不该插入注释的地方插入了注释。
以下语法有效,您的无效:
- name: Change the hostname
lineinfile:
dest: /etc/hosts
# regexp: '.*{{ item }}$'
regexp: '{{ hostvars[item].ansible_default_ipv4.address }}'
state: absent
# line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
# state: present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
Here you go..
---
- name: host trick
hosts: dev
gather_facts: yes
become: true
pre_tasks:
- name: Include fixed env variables
include_vars: "group_vars/dev.yml"
tasks:
- debug: var=hostvars[groups['app'][0]].ansible_host
- name: Update the /etc/hosts file with node name
vars:
remove_host: "hostname.domain.com"
tags: etchostsupdate
become: yes
become_user: root
lineinfile:
dest: /etc/hosts
regexp: "{{ remove_host }}"
line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}}"
state: absent
backup: yes
register: etchostsupdate
when: hostvars[item]['ansible_facts']['default_ipv4'] is defined
with_items:
- "{{ groups['dev'] }}"
#ansible-playbook -i dev/hosts dev/remove_etc_hosts.yml -e "remove_host=hostname.domain.com"
我正在尝试更改远程节点的主机名并且此部分有效:
- name: Change the hostname
lineinfile: dest=/etc/hosts
regexp='.*{{ item }}$'
line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
因此,它会将 IP hostname FQDN
附加到 /etc/hosts
文件的末尾。
我想要实现的是删除现有条目然后添加此部分,这是我尝试过的:
- name: Change the hostname
lineinfile: dest=/etc/hosts
# regexp='.*{{ item }}$'
regexp='{{ hostvars[item].ansible_default_ipv4.address }}'
state=absent
# line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
# state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
但是,这一直显示以下错误:
The offending line appears to be:
# regexp='.*{{ item }}$'
regexp="{{ hostvars[item].ansible_default_ipv4.address }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.
将引号从 ''
更改为 ""
似乎不起作用。我的问题是:
- 这样查询IP是否正确,以便删除 主机文件中的条目?
- 如果是这样,有人可以指出错误 句法?
- 有没有更高效的方法来实现去除任务
(所有)现有条目,然后在
/etc/hosts
? 中添加行
您不能使用 Ansible 表示法(带等号)并将其视为 YAML。
您的代码的问题不在于引用,而在于您在不该插入注释的地方插入了注释。
以下语法有效,您的无效:
- name: Change the hostname
lineinfile:
dest: /etc/hosts
# regexp: '.*{{ item }}$'
regexp: '{{ hostvars[item].ansible_default_ipv4.address }}'
state: absent
# line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
# state: present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: "{{ groups['dbservers'] }}"
Here you go..
---
- name: host trick
hosts: dev
gather_facts: yes
become: true
pre_tasks:
- name: Include fixed env variables
include_vars: "group_vars/dev.yml"
tasks:
- debug: var=hostvars[groups['app'][0]].ansible_host
- name: Update the /etc/hosts file with node name
vars:
remove_host: "hostname.domain.com"
tags: etchostsupdate
become: yes
become_user: root
lineinfile:
dest: /etc/hosts
regexp: "{{ remove_host }}"
line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}}"
state: absent
backup: yes
register: etchostsupdate
when: hostvars[item]['ansible_facts']['default_ipv4'] is defined
with_items:
- "{{ groups['dev'] }}"
#ansible-playbook -i dev/hosts dev/remove_etc_hosts.yml -e "remove_host=hostname.domain.com"