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

查看:81
本文介绍了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

但是,我还有另外两个指定为Web服务器的服务器:

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,依此类推.

The aim is to name them as web01.domain, web02.domain and so on.

根据 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 .

Also there is such thing as with_indexed_items.

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

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