Ansible playboos 实现上传文件创建crontab

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible playboos 实现上传文件创建crontab相关的知识,希望对你有一定的参考价值。

需求:
上传 3 个 shell 脚本到指定目录下,设置权限为755,属主、组为 root
并且在 crontab 下设置

* * * * * /path/file

涉及到机器较多,遂使用 Ansible 去批量执行。
涉及到 copy 、cron 模块

copy 例子:

Examples

- name: example copying file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

cron 例子:

Examples

# Ensure a job that runs at 2 and 5 exists.
# Creates an entry like "0 5,2 * * ls -alh > /dev/null"
- cron:
    name: "check dirs"
    minute: "0"
    hour: "5,2"
    job: "ls -alh > /dev/null"

最后方案如下:

---
- hosts: ctijk
  remote_user: toptea
  become: yes
  become_method: su

tasks:
- name: transfer file to server
  copy: 
    src: /tmp/iomonitor.sh
    dest: /opt/iomonitor.sh
    owner: root
    group: root
    mode: 0755

- name: transfer file to server
  copy: 
    src: /tmp/topmonitor.sh
    dest: /opt/topmonitor.sh
    owner: root
    group: root
    mode: 0755

- name: transfer file to server
  copy: 
    src: /tmp/cpumonitor.sh
    dest: /opt/cpumonitor.sh
    owner: root
    group: root
    mode: 0755

- cron:
    name: "cti cpumonitor"
    minute: ‘*‘
    job:  "/opt/cpumonitor.sh"

- cron:
    name: "cti iomonitor"
    minute: ‘*‘
    job:  "/opt/iomonitor.sh"

- cron:
    name: "cti topmonitor"
    minute: ‘*‘
    job:  "/opt/topmonitor.sh"

以上是关于Ansible playboos 实现上传文件创建crontab的主要内容,如果未能解决你的问题,请参考以下文章

ansible PlayBook

ansible 通过网络下载和上传文件

Ansible项目实战搭建lnmp

Ansible项目实战搭建lnmp

通过gitlab-runner使用ansible对远程主机执行docker操作

RHCE-B13. 创建Ansible密码库,使用文件内密码加密yml