一、使用ansible playbook
- hosts: redhat7
vars:
selinux_policy: targeted
selinux_state: disabled
tasks:- name: Change Hostname
hostname:
name: “{{ ansible_default_ipv4.address.split(‘.’)[-2] }}-{{ ansible_default_ipv4.address.split(‘.’)[-1] }}” #以点为单位获取ip地址中倒数第二个字段和倒数一第一个字段,假如IP地址为192.168.10.10,执行完脚本之后主机名变为:10-10
- name: Change Hostname