To provision new Linux VMs with a working DNS configuration that fits our environment, I created the small Ansible role resolv.conf, which I would like to introduce below.

The role resolv.conf

This role is minimalistic and includes only the mandatory directories and files:

# tree roles/resolv.conf
roles/resolv.conf/
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
└── templates
    └── resolv.conf.j2

tasks/main.yml

To be able to manage the file /etc/resolv.conf with Ansible, I first had to stop the NetworkManager from doing so. For this task, I used the Ansible module ini_file, which sets the required option dns=none in the section [main]:

---
- name: make sure line 'dns=none' is set in /etc/NetworkManager/NetworkManager.conf
  ini_file:
    path: /etc/NetworkManager/NetworkManager.conf
    state: present
    no_extra_spaces: yes
    section: main
    option: dns
    value: none
    owner: root
    group: root
    mode: 0644
    backup: yes
  notify:
    - reload NetworkManager

The second task uses the template module to create the target configuration from the contents of roles/resolv.conf/templates/resolv.conf.j2, and place it in /etc/resolv.conf on the target node:

---
- name: deploy resolv.conf template
  template:
    src: roles/resolv.conf/templates/resolv.conf.j2
    dest: /etc/resolv.conf
    owner: root
    group: root
    mode: 0644
    backup: yes
  notify:
    - reload NetworkManager

Currently, my template contains static text. I could have used the copy module to copy this file to the target node. However, I used the template module to keep the possibility open for me to create the content dynamically by using variables.

With notify: a handler named reload NetworkManager is called twice in this playbook. I’ll cover handlers in the next section.

handlers/main.yml

 

Handlers are used to trigger actions that only execute if a task makes changes to the target node. These handlers are only processed at the end of a playbook and are only executed once, even if they were notified of changes by several tasks.

In the example described in this text, the handler named reload NetworkManager executes the defined task, but only if one of the two tasks (or both) from tasks/main.yml has led to a change on the target node:

# cat resolv.conf/handlers/main.yml
---
- name: reload NetworkManager
  service:
    name: NetworkManager
    state: reloaded

Note that the handlers are not executed until all tasks have been successfully processed. In some cases this fact can make troubleshooting more difficult. I found an example in the book Ansible: Up and Running, by Rene Moser and Lorin Hochstein, which I would like to share here. Imagine the following procedure:

  1. You are running a playbook.
  2. One of the tasks uses notify on changes.
  3. In a following task, an error occurs that causes the processing to abort.
  4. You fix the problem and run the playbook again.

The task from step two already made its changes successfully. When the playbook is executed again, its status will therefore be OK and not CHANGED. However, the handler was not executed, because the processing aborted before that point. The handler will not be triggered when the playbook is executed again, since the task required for this no longer leads to any changes in the target node.

Hochstein writes that handlers are mostly used to restart services or reload their configuration. This can, of course, also be achieved without the use of handlers by explicitly restarting a service at the end of the Playbook. Which is the better way, may everyone decide for themselves.

Conclusion

This was one of my first Ansible roles ever. Whether this is the smartest way to create the DNS configuration, or whether there are even more elegant approaches, I don’t know. At least this solution seems to be robust and hasn’t let me down yet.


关于作者

Jörg has been a Sysadmin for over ten years now. His fields of operation include Virtualization (VMware), Linux System Administration and Automation (RHEL), Firewalling (Forcepoint), and Loadbalancing (F5). He is a member of the Red Hat Accelerators Community and author of his personal blog at https://www.my-it-brain.de.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来