ansible通過使用delegate_to關鍵字委派任務到指定的機器上運行
使用示例:
1、hosts配置
- name: add host record to DC server
shell: 'echo "192.168.10.12 node2" >> /etc/hosts'
- name: add host record to all server
shell: 'echo "192.168.10.12 node2 " >> /etc/hosts'
delegate_to: 192.168.10.100
2、ntp-server配置
ntp-server執行server配置,nep-client執行定時任務同步server。
- name: install ntp
shell: yum -y install ntp
- name: stop all ntp_install server
shell: systemctl stop ntpd && systemctl disable ntpd
- name: config ntp server
template: src=templates/ntp.conf dest=/etc/ntp.conf
run_once: true
delegate_to: "{{ntp.ntp_server}}"
- name: start ntp server
shell: systemctl start ntpd && systemctl enable ntpd
run_once: true
delegate_to: "{{ntp.ntp_server}}"
- name: add crontab
cron: name='syn time' minute=*/1 job='/usr/sbin/ntpdate -u {{ntp.ntp_server}} ; /usr/sbin/hwclock -w'
- name: reload crond.service
shell: systemctl reload crond.service
ansible通過使用local_action關鍵字委派任務在本機運行
使用示例:
1、hosts配置
- name: add host record to localhost
local_action: shell 'echo "192.168.10.12 node2 " >> /etc/hosts'
參考:https://www.ibm.com/developerworks/cn/linux/1608_lih_ansible/index.html