用于将连续整数分配为主机名的 Ansible 循环 [英] Ansible loops for assigning sequential integers as hostnames

查看:21
本文介绍了用于将连续整数分配为主机名的 Ansible 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ansible 的新手.我有以下更改远程服务器主机名的剧本:

I'm new to Ansible. I have the following playbook that changes the hostname for a remote server:

---
- hosts: dbservers
  remote_user: testuser1
  become: yes
  become_method: sudo

  vars: 
    LOCAL_HOSTNAME: 'db01'
    LOCAL_DOMAIN_NAME: 'ansibletest.com'

  tasks:
    # Checks and removed the existing occurences of <IP hostname FQDN> from /etc/hosts
    - name: Remove occurences of the existing IP
      lineinfile: dest=/etc/hosts
                  regexp='{{ hostvars[item].ansible_default_ipv4.address }}'
                  state=absent
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    #  Adds the IP in the format  <IP hostname FQDN> to /etc/hosts
    - name: Add the IP and hostname to the hosts file
      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'] }}"

    - name: Remove HOSTNAME occurences from /etc/sysconfig/network
      lineinfile: dest=/etc/sysconfig/network
                  regexp='^HOSTNAME'
                  state=absent
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    - name: Add new HOSTNAME to /etc/sysconfig/network
      lineinfile: dest=/etc/sysconfig/network
                  regexp='^HOSTNAME='
                  line="HOSTNAME={{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
                  state=present
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    - name: Set up the hostname
      hostname: name={{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}

在这个例子中,LOCAL_HOSTNAME 已经被赋值为 db01.在这种情况下,dbservers 组只有一台服务器:

In this example, LOCAL_HOSTNAME is already assigned a value of db01. And in this scenario, the dbservers group has only one server:

[dbservers]
192.168.1.93

但是,我还有另外两台服务器被指定为网络服务器:

However, I also have 2 other servers that are designated to be webservers:

[webservers]
192.168.1.95
192.168.1.96

[dbservers]
192.168.1.93

目的是将它们命名为web01.domainweb02.domain等.

根据 docs 看来这可以通过使用with_sequence来实现.

As per the docs it seems that this could be achieved by using with_sequence.

我的问题是,是否可以(在 Ansible 中)在循环中使用 2 个变量?类似于以下伪代码的内容:

My question is, is it possible (in Ansible) to use 2 variables in loops? Something along the lines of the pseudo-code below:

i=1
for host in webservers:
   open host(/etc/hosts):
       add "IP<space>HOSTNAME{i}<space>"<space>"HOSTNAME{i}.FQDN"
       i++

这可以使用剧本来实现还是我以错误的方式解决问题?

Could this be achieved using playbooks or am I approaching the issue in an wrong way?

推荐答案

首先生成索引主机名,将其定义为 hostfact 并稍后使用它来填充其他服务器的主机文件.

Generate indexed hostname first, define it as hostfact and use it later to fill other servers' hosts files.

- hosts: webservers
  gather_facts: no
  tasks:
    - set_fact:
        indexed_hostname: "{{ 'web{0:02d}'.format(play_hosts.index(inventory_hostname)+1) }}"

- hosts: dbservers
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ hostvars[item].indexed_hostname }}"
      with_items: "{{ groups['webservers'] }}"

还有with_indexed_items.

这篇关于用于将连续整数分配为主机名的 Ansible 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆