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

24.15 ansible介绍html

24.16 ansible安装python

24.17 ansible远程执行命令linux

24.18 ansible拷贝文件或目录git

24.19 ansible远程执行脚本github

24.20 ansible管理任务计划web

 

 

 

 

24.15 ansible介绍shell

 

 

 

不须要安装客户端,经过sshd去通讯vim

基于模块工做,模块能够由任何语言开发centos

不只支持命令行使用模块,也支持编写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

相关文章
相关标签/搜索