【问题标题】:Ansible regex to delete matching IPs from /etc/hostsAnsible 正则表达式从 /etc/hosts 中删除匹配的 IP
【发布时间】:2017-01-12 10:22:01
【问题描述】:

我正在尝试更改远程节点的主机名,这部分有效:

- 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? 中添加该行?

【问题讨论】:

标签: regex linux ansible devops


【解决方案1】:

您不能使用 Ansible 表示法(带有等号)并将其视为 YAML。

你的代码的问题不是引用,而是你在不应该插入的地方插入了 cmets。

以下语法是有效的,你的不是:

- 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'] }}"

【讨论】:

    【解决方案2】:

    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"

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2010-09-09
      • 2011-07-08
      • 1970-01-01
      相关资源
      最近更新 更多