Ansibleはdelegate_toキーワードを使用して、タスクを指定されたマシンに委任して実行します
使用例:
1。ホスト構成
- 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はサーバー構成を実行し、nep-clientはサーバーを同期するためのタイミングタスクを実行します。
- 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。ホスト構成
- 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