95.ansible介绍 安装 命令 拷贝 执行脚本 crontab

24.15 ansible介绍

24.16 ansible安装

24.17 ansible远程执行命令

24.18 ansible拷贝文件或目录

24.19 ansible远程执行脚本

24.20 ansible管理任务计划

 

 

 

 

24.15 ansible介绍

 

 

 

不需要安装客户端,通过sshd去通信

基于模块工作,模块可以由任何语言开发

不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读

安装十分简单,centos上可直接yum安装

playbook类似于saltstack的top.sls,top.sls又指向了一些子配置文件

有提供UI(浏览器图形化)www.ansible.com/tower,收费的

官方文档 http://docs.ansible.com/ansible/latest/index.html

ansible已经被redhat公司收购,它在github上是一个非常受欢迎的开源软件,github地址https://github.com/ansible/ansible

一本不错的入门电子书 https://ansible-book.gitbooks.io/ansible-first-book/

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

24.16 ansible安装

 

 

 

 

1.准备两台机器,前面我们做实验的两台机器aming-01,aming-02

2.只需要在aming-01上安装ansible

yum list |grep ansible 可以看到自带源里就有2.4版本的ansible

yum install -y ansible

3.aming-01上生成密钥对 ssh-keygen -t rsa(表示-t生成类型为rsa)

4.把公钥放到aming-02上,设置密钥认证

5.vi /etc/ansible/hosts //增加(配置主机组)。管理机器的时候可以把他们分成多个组,比如web组、db组,每一个组里面都要若干个机器。后期可以针对主机组去做操作

[testhost]

127.0.0.1

192.168.208.130

说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。

 

 

实例:

[root@axinlinux-01 ~]# yum list |grep ansible

ansible.noarch 2.7.4-1.el7 epel

ansible-doc.noarch 2.7.4-1.el7 epel #文档

[root@axinlinux-01 ~]# yum install -y ansible ansible-doc #两个都安装上

[root@axinlinux-01 ~]# ls /root/.ssh/ #因为之前有做过,有这两个文件。如果没有就ssh-keygen来生成

authorized_keys id_rsa id_rsa.pub known_hosts

[root@axinlinux-01 ~]# cat /root/.ssh/id_rsa.pub #复制01机器的公钥

[root@axinlinux-02 ~]# vim /root/.ssh/authorized_keys #将复制的公钥放到这里

[root@axinlinux-01 ~]# vim /root/.ssh/authorized_keys #本机上也要公钥

[root@axinlinux-01 ~]# ssh axinlinux-02 #登录是否成功

[root@axinlinux-01 ~]# vim /etc/ansible/hosts #配置主机组

[testhost]

127.0.0.1

axinlinux-02 #此处也可以是ip,主机名的话要设置一下/etc/hosts

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

24.17 ansible远程执行命令

 

 

 

1.ansible  testhost -m command -a 'w' 

#testhost为主机组里的机器

-m指的是模块

-a指的是什么命令

2.这样就可以批量执行命令了。这里的testhost 为主机组名,-m后边是模块名字,-a后面是命令。当然我们也可以直接写一个ip,针对某一台机器来执行命令。

ansible 127.0.0.1 -m command -a 'hostname'

错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"

解决: yum install -y libselinux-python

3.还有一个模块就是shell同样也可以实现 (比如远程执行一个shel脚本,也支持想要一个命令)

ansible  testhost -m shell -a 'w'

 

 

 

实例:

[root@axinlinux-01 ~]# ansible testhost -m command -a 'w'

[root@axinlinux-01 ~]# ansible testhost -m command -a 'hostname'

axinlinux-02 | CHANGED | rc=0 >>

axinlinux-02

 

axinlinux-01 | CHANGED | rc=0 >>

axinlinux-01

[root@axinlinux-01 ~]# ansible axinlinux-02 -m command -a 'hostname' #也可以仅看一台机器

axinlinux-02 | CHANGED | rc=0 >>

axinlinux-02

[root@axinlinux-01 ~]# ansible testhost -m shell -a 'hostname' #shell模块也可以写一个命令

axinlinux-02 | CHANGED | rc=0 >>

axinlinux-02

 

axinlinux-01 | CHANGED | rc=0 >>

axinlinux-01

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

24.18 ansible拷贝文件或目录

 

 

 

1.ansible aming-02 -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0755" (拷贝目录)

#copy是拷贝模块,可以拷贝文件、目录

src指定来源的文件或目录是谁

dest目标,来源是文件,那目标也要是文件

owner指定属主

mode权限,也可以写755

注意:源目录会放到目标目录下面去,如果目标指定的目录不存在,它会自动创建。如果拷贝的是文件,dest指定的名字和源如果不同,并且它不是已经存在的目录,相当于拷贝过去后又重命名。但相反,如果desc是目标机器上已经存在的目录,则会直接把文件拷贝到该目录下面。

2.ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123" (拷贝文件)

这里的/tmp/123和源机器上的/etc/passwd是一致的,但如果目标机器上已经有/tmp/123目录,则会再/tmp/123目录下面建立passwd文件

 

 

实例:

[root@axinlinux-01 ~]# ansible axinlinux-02 -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=755"

axinlinux-02 | CHANGED => {

"changed": true,

"dest": "/tmp/ansibletest/",

"src": "/etc/ansible"

}

[root@axinlinux-02 ~]# ls /tmp/ansibletest/ansible/ #看一下02机器是否有。被创建了目标目录

ansible.cfg hosts roles/

[root@axinlinux-02 ~]# ls -ld /tmp/ansibletest/

drwxr-xr-x 3 root root 21 12月 9 22:32 /tmp/ansibletest/

[root@axinlinux-02 ~]# date

2018年 12月 09日 星期日 22:34:07 CST

[root@axinlinux-01 ~]# ansible axinlinux-02 -m copy -a "src=/etc/passwd dest=/tmp owner=root group=root mode=755"

axinlinux-02 | CHANGED => {

"changed": true,

"checksum": "eea17158509c38ed0c80faae566407c920c47c31",

"dest": "/tmp/passwd",

"gid": 0,

"group": "root",

"md5sum": "4a6c40d0208f75636981b494f55109ea",

"mode": "0755",

"owner": "root",

"size": 2374,

"src": "/root/.ansible/tmp/ansible-tmp-1544366308.12-248977953608911/source",

"state": "file",

"uid": 0

}

[root@axinlinux-02 ~]# ls -ld /tmp/passwd

-rwxr-xr-x 1 root root 2374 12月 9 22:38 /tmp/passwd

[root@axinlinux-02 ~]# date

2018年 12月 09日 星期日 22:40:06 CST

[root@axinlinux-01 ~]# ansible axinlinux-02 -m copy -a "src=/etc/passwd dest=/tmp/1.txt owner=root group=root mode=755" #改名字的话,直接指定目标文件的名字即可。跟cp命令差不多

[root@axinlinux-02 ~]# ls -ld /tmp/1.txt

-rwxr-xr-x 1 root root 2374 12月 9 22:42 /tmp/1.txt

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

24.19 ansible远程执行脚本

 

 

 

1.首先创建一个shell脚本

vim  /tmp/test.sh  //加入内容

#!/bin/bash

echo `date` > /tmp/ansible_test.txt #将系统时间输出到这个文件里

2.然后把该脚本分发到各个机器上(ansible需要将脚本放在相应的机器上才可以执行)

ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"

#脚本的权限要是755才可以执行

3.最后是批量执行该shell脚本

ansible testhost -m shell -a "/tmp/test.sh"

shell模块,还支持远程执行命令并且带管道 #command不支持

ansible testhost -m shell -a "cat /etc/passwd|wc -l "

 

 

 

实例:

[root@axinlinux-01 ~]# vim /tmp/test.sh

#!/bin/bash

echo `date` > /tmp/ansible_test.txt

[root@axinlinux-01 ~]# ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" #将脚本分发到各个机器上去

[root@axinlinux-01 ~]# ansible testhost -m shell -a "/tmp/test.sh" #执行各个机器上的test.sh脚本

axinlinux-02 | CHANGED | rc=0 >>

 

 

axinlinux-01 | CHANGED | rc=0 >>

[root@axinlinux-01 ~]# ls /tmp/ansible_test.txt #检查是否创建了脚本里的文件

/tmp/ansible_test.txt

[root@axinlinux-01 ~]# cat !$ #内容

cat /tmp/ansible_test.txt

2018年 12月 09日 星期日 22:53:17 CST

[root@axinlinux-01 ~]# ansible testhost -m command -a "cat /etc/passwd|wc -l" #command不支持管道

axinlinux-02 | FAILED | rc=1 >>

cat:无效选项 -- l

Try 'cat --help' for more information.non-zero return code

 

axinlinux-01 | FAILED | rc=1 >>

cat:无效选项 -- l

Try 'cat --help' for more information.non-zero return code

 

[root@axinlinux-01 ~]# ansible testhost -m shell -a "cat /etc/passwd|wc -l" #shell支持管道

axinlinux-02 | CHANGED | rc=0 >>

31

 

axinlinux-01 | CHANGED | rc=0 >>

46

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

24.20 ansible管理任务计划

 

 

 

1.ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt'  weekday=6"

#-m cron 用到了cron模块

name指定任务计划的名字

job指定命令是什么

weekday=6就是星期六,也就是这里指定分时日月周,不指定的话就是星

2.若要删除该cron 只需要加一个字段 state=absent 

ansible testhost -m cron -a "name='test cron' state=absent"

 

3.其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month

4.不要手动的crontab -e去更改里面的东西,一旦更改了,就没法利用ansible去管理使用了。

 

 

实例:

[root@axinlinux-01 ~]# ansible axinlinux-02 -m cron -a"name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"

axinlinux-02 | CHANGED => {

"changed": true,

"envs": [],

"jobs": [

"test cron"

]

}

[root@axinlinux-02 ~]# crontab -l

# Lines below here are managed by Salt, do not edit

#Ansible: test cron #这就是我们定义的名字

* * * * 6 /bin/touch /tmp/1212.txt

[root@axinlinux-01 ~]# ansible axinlinux-02 -m cron -a"name='test cron' state=absent" #删除这个任务计划

axinlinux-02 | CHANGED => {

"changed": true,

"envs": [],

"jobs": []

}

[root@axinlinux-02 ~]# crontab -l

# Lines below here are managed by Salt, do not edit

[root@axinlinux-01 ~]# ansible axinlinux-02 -m cron -a"name='test cron' job='/bin/touch /tmp/1212.txt' minute=20 hour=10 weekday=6"

axinlinux-02 | CHANGED => {

"changed": true,

"envs": [],

"jobs": [

"test cron"

]

}

[root@axinlinux-02 ~]# crontab -l

# Lines below here are managed by Salt, do not edit

#Ansible: test cron

20 10 * * 6 /bin/touch /tmp/1212.txt

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3866149/blog/3030850

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值