05.12 ansible批量自动化管理工具,05.12ansible


第一章 ansible软件知识介绍


第二章 ansible软件特点描述


第三章 ansible借助公钥批量管理

利用非交互式工具实现批量分发公钥与批量管理服务器

sshpass -p123456 ssh -o StrictHostKeyChecking=no  172.16.1.7 hostname
-p 指定ssh连接用户的密码
-o StrictHostKeyChecking=no 避免第一次登陆出现公钥检查

第四章 ansible软件安装部署

搭建yum仓库,定制rpm包时自动化运维关键内容;保留yum安装的软件

sed –i.bak ‘s#keepcache=0#keepcache=1#g’ /etc/yum.conf
grep keepcache /etc/yum.conf

第五章 ansible安装源

yum install epel-release   随意的一个epel源
wget –O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

更新下载epel,ansible软件需要借助epel源进行下载安装

所有被管理端需要安装libselinux-python软件
yum install –y ansible

第六章 ansible配置文件修改## 标题 ##

修改配置文件添加模块

[root@m01 script]# vim /etc/ansible/hosts
[oldboy]
172.16.1.41
172.16.1.31
172.16.1.7

第七章 ansible远程批量拷贝信息

[root@m01 script]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/ owner=oldboy group=oldboy"

如果指定的目标目录不存在,熊会自动创建,否则源目录会被放到目标下面去。
如果copy的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于copy过去后再重命名。
如果dest是目标机器上已经存在的目录,则会直接把文件copy到该目录下面。
设定的用户和组oldboy在所有客户端必须存在。


第八章 ansible常用参数

命令参数 参数说明
-m MODULE_NAME 相对应名称的的模块被执行(默认模块为command) -m后边是模块的名字
-a MODULE_ARGS 模块参数信息;-a后面是要执行的命令里也可以写一个ip,针对一台机器来执行命令
-C check 不做任何改变;反而是尝试语言一些可能出现的改变

第九章 ansible远程批量执行脚本

[root@m01 script]# ansible oldboy -m script -a "2.sh"       利用shell模块执行脚本文件
[root@m01 script]# ansible oldboy -m shell -a "2.sh"        利用script模块执行脚本
[root@m01 script]# ansible oldboy -m shell -a "tree /tmp/"  利用shell模块执行命令信息

sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 172.16.1.41"
说明:
Script模块与shell模块区别
Shell需要将脚本文件复制到远程服务器,然后执行远程服务器的脚本
Script不需要将脚本文件复制到远程服务器,实质是将脚本执行过程在远程服务器上进行执行

第十章 ansible查看软件帮助信息

ansible-doc -l             ##列出所有的模块
ansible-doc-s service      ##查看指定模块用法

第十一章 如何完成集群规模架构一键自动化实现


第十二章 ansible软件特点/核心功能


第十三章 ansible软件进阶-定时任务

查看帮助
[root@m01 yum]# ansible-doc -s cron

1. 添加定时任务每5分钟执行

[root@ansible01-61 ~]# ansible 172.16.1.111 -m cron -a "minute=*/5 hour=* job='/usr/sbin/update ntp1.aliyun.com >/dev/null'"
172.16.1.111 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "None"
    ]
}

2. 查看定时任务列表

[root@ansible01-61 ~]# ansible 172.16.1.111 -m command -a "crontab -l"
172.16.1.111 | SUCCESS | rc=0 >>
#Ansible: None
*/5 * * * * /usr/sbin/update ntp1.aliyun.com >/dev/null

[root@ansible01-61 ~]# ansible 172.16.1.111 -a "crontab -l"
172.16.1.111 | SUCCESS | rc=0 >>
#Ansible: None
*/5 * * * * /usr/sbin/update ntp1.aliyun.com >/dev/null

3. 添加定时任务每分钟执行

[root@ansible01-61 ~]# ansible 172.16.1.111 -m cron -a "name='aliyun time Synchronize' minute=* hour=* job='/usr/sbin/update ntp1.aliyun.com >/dev/null'"
172.16.1.111 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "None", 
        "aliyun time Synchronize"
    ]
}

4. 添加定时任务名称

[root@m01 yum]# ansible 172.16.1.31 -m cron -a "name='time crontab' minute=*/5  hour=* day=* month=* weekday=* job='/usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2>&1'"

5. -C代表ansible测试

[root@m01 yum]# ansible 172.16.1.31 -C  -m cron -a "name='oldboy crontab' minute=*/5  hour=* day=* month=* weekday=* job='/usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2>&1'

6. 删除定时任务

[root@ansible01-61 ~]# ansible 172.16.1.111 -m cron -a "name='None' state=absent"
172.16.1.111 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "aliyun time Synchronize"
    ]
}

7. 注释定时任务 disabled=yes

[root@ansible01-61 ~]# ansible 172.16.1.111 -m cron -a "name='aliyun time Synchronize' disabled=yes job='/usr/sbin/update ntp1.aliyun.com >/dev/null'"
172.16.1.111 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "aliyun time Synchronize"
    ]
}
[root@ansible01-61 ~]# ansible 172.16.1.111 -a "crontab -l"
172.16.1.111 | SUCCESS | rc=0 >>
#Ansible: aliyun time Synchronize
#* * * * * /usr/sbin/update ntp1.aliyun.com >/dev/null

8. 解除注释定时任务 disabled=no

[root@ansible01-61 ~]# ansible 172.16.1.111 -m cron -a "name='aliyun time Synchronize' disabled=no job='/usr/sbin/update ntp1.aliyun.com >/dev/null'"
172.16.1.111 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "aliyun time Synchronize"
    ]
}
[root@ansible01-61 ~]# ansible 172.16.1.111 -a "crontab -l"
172.16.1.111 | SUCCESS | rc=0 >>
#Ansible: aliyun time Synchronize
* * * * * /usr/sbin/update ntp1.aliyun.com >/dev/null

第十四章 ansible重要模块总结

1. command

[root@m01 ~]# ansible oldboy -a "df -h"  默认省略
[root@m01 ~]# ansible oldboy -m command -a "df -h"

2. shell

[root@m01 ~]# ansible oldboy -m shell -a "echo oldboy_linux >>/tmp/123.txt"
[root@m01 ~]# ansible oldboy -m shell -a "cat /tmp/123.txt"

3. script

[root@m01 script]# ansible oldboy -m script -a "abc.sh"

4. copy

[root@m01 script]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/"

5. cron

[root@m01 script]# ansible oldboy -m cron -a "name=ceshi minute=*/5 job='/bin/bash /usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2>&1'"

[root@m01 script]# ansible oldboy -m cron -a "name=ceshi state=absent"

6. ping

[root@m01 script]# ansible oldboy -m ping

7. yum

[root@m01 script]# ansible oldboy -m yum -a "name=namp state=installed"

8. service

[root@m01 script]# ansible oldboy -m service -a "name=crond state=stopped"
[root@m01 script]# ansible oldboy -m service -a "name=crond state=started"

9. file

[root@m01 script]# ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/hosts state=link"

第十五章 ansible剧本编写规则

1. pyYAML剧本编写规则
缩进:
yaml使用一个固定的缩进风格表示数据层结构关系,saltstack需要每个缩进级别由两个空格组成,一定不能使用tab键。

冒号:
每个冒号后面一定有一个空格(以冒号结尾不需要空格,表示文件路径的模板可以不需要空格)。

短横线:
表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分。

2. 编写剧本过程 编写好之后先 -C 测试 然后再执行

[root@m01 playbook_dir]# vim command.yml
- hosts: 172.16.1.31
  tasks:
- command: uptime

[root@m01 playbook_dir]# ansible-playbook command.yml

PLAY [172.16.1.31] *******************************************************************************

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

TASK [command] ***********************************************************************************
changed: [172.16.1.31]

PLAY RECAP ***************************************************************************************
172.16.1.31                : ok=2    changed=1    unreachable=0    failed=0   

3. 追加文本内容 使用shell模块

[root@m01 playbook_dir]# vim command.yml
- hosts: 172.16.1.31
  tasks:
    - shell: echo "oldboy-linux" >>/etc/hosts

4. –syntax-check 检查语法

ansible-playbok  --syntax-check /etc/ansible/playbook_dir/command.yml

相关内容

    暂无相关文章