每日学习-ansible任务委派与本地操作

2021-01-24 08:28发布

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

标签: