I want to insert the following entry into the /etc/hosts file somehow using Ansible.
10.33.5.44 ip-10-33-5-44
Run Code Online (Sandbox Code Playgroud)
Here, the schema is that the ip should have an alias corresponding to the IP, prefixed with ip- and where the dot . would be replaced by dash -.
But to get this IP, I can only think of doing the host command on a DNS name e.g.
host euwest2-test-box.company.com
> euwest2-test-box.company.com has address 10.33.5.44
Run Code Online (Sandbox Code Playgroud)
Can anyone suggest how to get that to work? Is it possible?
您可以使用dig查找来实现此目的。然后在主机文件中添加以下行lineinefile。
请注意,该模块dig需要 Python 库dnspython才能运行。因此您可能还想与 Ansible 一起安装它。
因此,根据剧本:
- hosts: all
gather_facts: no
tasks:
- package:
name: py-dnspython
state: present
- lineinfile:
path: /etc/hosts
line: "{{ item }} ip-{{ item | replace('.', '-') }}"
loop: "{{ lookup('dig', 'stackoverflow.com.').split(',') }}"
Run Code Online (Sandbox Code Playgroud)
这给出了回顾:
PLAY [all] *********************************************************************************************
TASK [package] *****************************************************************************************
changed: [localhost]
TASK [lineinfile] **************************************************************************************
changed: [localhost] => (item=151.101.1.69)
changed: [localhost] => (item=151.101.65.69)
changed: [localhost] => (item=151.101.129.69)
changed: [localhost] => (item=151.101.193.69)
PLAY RECAP *********************************************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Run Code Online (Sandbox Code Playgroud)
并相应地填充主机文件:
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 21eef8264e0c
151.101.1.69 ip-151-101-1-69
151.101.65.69 ip-151-101-65-69
151.101.129.69 ip-151-101-129-69
151.101.193.69 ip-151-101-193-69
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10820 次 |
| 最近记录: |