Ansible - 在主机上每次 运行 时在变量中添加 1
Ansible - Add 1 in variable each time running on host
我正在尝试制作一个可以更改多个主机的主机名的剧本。因此,每次任务在主机上运行时,都必须在名称中添加 +1。所以主机名 1、主机名 2 等
---
- name: Hostname
hosts: win
vars:
winname: server
count: 1
tasks:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "{{ winname }}{{ count }}"
register: reboot
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
when: reboot.reboot_required
# Add 1
- set_fact:
count: "{{ count +1}}"
我试过了,但计数保持在 1。有人知道我该如何解决这个问题吗?
谢谢!
您的场景需要使用递增的主机名逐个主机依次执行*。所以试试下面的方法
- name: set count
set_fact:
count: 1
- name: Hostname Update Block
block:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "server{{ count }}"
register: reboot
delegate_to: {{ item }}
run_once: true
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
delegate_to: {{ item }}
run_once: true
when: reboot.reboot_required
# Count Increment
- name: increase count
set_fact: count={{ count | int + 1 }}
with_items: groups['win']
在Krishna Reddy的回答后我这样解决了:
我没有使用块,而是包含了一个单独的文件。
---
- name: Hostname
hosts: win
tasks:
- name: set count
set_fact:
count: 1
- include: hostnamesub.yml
with_items: "{{ groups['win'] }}"
然后在单独的文件中:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "server{{ count }}"
register: reboot
delegate_to: "{{ item }}"
run_once: true
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
delegate_to: "{{ item }}"
run_once: true
when: reboot.reboot_required
# Count Increment
- name: increase count
set_fact: count={{ count | int + 1 }}
我正在尝试制作一个可以更改多个主机的主机名的剧本。因此,每次任务在主机上运行时,都必须在名称中添加 +1。所以主机名 1、主机名 2 等
---
- name: Hostname
hosts: win
vars:
winname: server
count: 1
tasks:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "{{ winname }}{{ count }}"
register: reboot
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
when: reboot.reboot_required
# Add 1
- set_fact:
count: "{{ count +1}}"
我试过了,但计数保持在 1。有人知道我该如何解决这个问题吗?
谢谢!
您的场景需要使用递增的主机名逐个主机依次执行*。所以试试下面的方法
- name: set count
set_fact:
count: 1
- name: Hostname Update Block
block:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "server{{ count }}"
register: reboot
delegate_to: {{ item }}
run_once: true
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
delegate_to: {{ item }}
run_once: true
when: reboot.reboot_required
# Count Increment
- name: increase count
set_fact: count={{ count | int + 1 }}
with_items: groups['win']
在Krishna Reddy的回答后我这样解决了: 我没有使用块,而是包含了一个单独的文件。
---
- name: Hostname
hosts: win
tasks:
- name: set count
set_fact:
count: 1
- include: hostnamesub.yml
with_items: "{{ groups['win'] }}"
然后在单独的文件中:
# Hostname edit
- name: Hostname edit
ansible.windows.win_hostname:
name: "server{{ count }}"
register: reboot
delegate_to: "{{ item }}"
run_once: true
# Reboot host
- name: Reboot
ansible.windows.win_reboot:
delegate_to: "{{ item }}"
run_once: true
when: reboot.reboot_required
# Count Increment
- name: increase count
set_fact: count={{ count | int + 1 }}