欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

day42-ansible模块补充及playbook初涉

程序员文章站 2022-05-11 11:12:21
...

ansible模块补充

serivce

重启crond服务

 ansible all -m service -a 'name=crond enabled=yes state=restarted'

更多参数

serivce
    开启 关闭重启
    name
    state
        started #启动服务
        stopped #停止服务
        restarted #重启服务
        reloaded #重载服务
    enabled
        yes
        no

cron模块

1.书写定时任务到所有主机
每5分钟同步系统时间

[aaa@qq.com ~]# ansible all -m cron -a 'name="sync time" minute="*/5" job="/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" state=present'

2.检查

[aaa@qq.com ~]# ansible all -a 'crontab -l'
172.16.1.7 | CHANGED | rc=0 >>
#Ansible: sync time
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

172.16.1.31 | CHANGED | rc=0 >>
#Ansible: sync time
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

172.16.1.41 | CHANGED | rc=0 >>
#Ansible: sync time
*/5 * * * * /sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

mount模块

day42-ansible模块补充及playbook初涉
[图片上传中...(QQ截图20190529113808.png-c2e7df-1559118392786-0)]

挂载NFS共享目录到web服务器
首先确保NFS端的nfs配置正常

[aaa@qq.com ~]# cat /etc/exports
#
/nfs  172.16.1.0/24(rw)
[aaa@qq.com ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/nfs   172.16.1.0/24

如果没有配置好,请参考nfs环境部署点击链接

把nfs01 共享的/nfs 挂载到 web01的/upload

[aaa@qq.com ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=mounted'
172.16.1.7 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/upload", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "172.16.1.31:/nfs"
}

检查

[aaa@qq.com ~]# ansible 172.16.1.7 -a 'df -h'
172.16.1.7 | CHANGED | rc=0 >>
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3            18G  1.7G   17G  10% /
devtmpfs            476M     0  476M   0% /dev
tmpfs               487M     0  487M   0% /dev/shm
tmpfs               487M   14M  473M   3% /run
tmpfs               487M     0  487M   0% /sys/fs/cgroup
/dev/sda1          1014M  138M  877M  14% /boot
tmpfs                98M     0   98M   0% /run/user/0
172.16.1.31:/nfs     18G  1.8G   17G  10% /upload

卸载

[aaa@qq.com ~]# ansible 172.16.1.7 -m mount -a 'fstype=nfs src=172.16.1.31:/nfs path=/upload state=absent'

playbook

基本格式

day42-ansible模块补充及playbook初涉
F}I%g

day42-ansible模块补充及playbook初涉
QQ截图20190529113808.png

编写剧本创建目录

[aaa@qq.com /etc/ansible]# vim touch.ym1 
---
  - hosts: all
    tasks:
    - name: make av
      command: mkdir -p /tmp/1/2/3

检查语法是否正确

[aaa@qq.com /etc/ansible]# ansible-playbook -C cron-sync.yml 

PLAY [all] ***************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]
ok: [172.16.1.7]

TASK [cron-sync] *********************************************************************************************************************
changed: [172.16.1.41]
changed: [172.16.1.31]
changed: [172.16.1.7]

PLAY RECAP ***************************************************************************************************************************
172.16.1.31                : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.41                : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.7                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0