我希望能够编写一本剧本来更新我在不同环境中的所有服务器 - 阶段和产品。我不想在每次内核更新时重新启动这些服务器,我只想每 2 个月重新启动一次。
有没有办法将重启变量传递给 ansible 命令以仅在我想重启时触发所有服务器的重启?
我将使用 Jenkins 来触发更新作业。
下面的示例代码:
- hosts: all
become: yes
tasks:
- name: Making changes to the yum.conf file
shell: sudo sed "s/exclude=kernel/#exclude=kernel/g" /etc/yum.conf
args:
executable: /bin/bash
- name: Performing yum update
yum:
update_cache: yes
name: '*'
state: latest
update_cache: yes
- name: Checking for reboot
shell: LAST_KERNEL=$(rpm -q --last kernel | awk 'NR==1{sub(/kernel-/,""); print $1}'); CURRENT_KERNEL=$(uname -r); if [ $LAST_KERNEL != $CURRENT_KERNEL ]; then echo 'reboot'; else echo 'no'; fi
ignore_errors: true
register: reboot_hint
- name: Rebooting servers now ...
command: shutdown -r now "Reboot required for updated kernel"
async: 0
poll: 0
sudo: true
ignore_errors: true
when: reboot_hint.stdout.find("reboot") != -1
register: rebooting
- name: Taking a nap while servers reboot...
pause: seconds=200
when: rebooting is changed
- name: Confirming servers are back online
wait_for:
host: "{{ ansible_ssh_host | default(inventory_hostname) }}"
delay: 30
state: started
search_regex: OpenSSH
port: 22
become: false
when: reboot is changed
delegate_to: localhost