ansible的精简用法,ansible精简用法


ansible的用途:
通过一个ansible的服务器去管理其他的多个服务器。
epel仓库的配置如下:

[epel]
name=Fedora EPEL
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64/
gpgcheck=0

前提:
ansible是利用ssh的,所以我们首先要将自动接收yes的ssh这个属性给打开,然后利用秘钥还是利用密码登录ssh这个看个人设置。如果是利用密码的形式则需要 在后面加 -k 如:ansible all -m copy -a “src=/app/hello dest=/app” -k 。秘钥则不需要。
ansible的安装及配置:

[root@localhost ~]# yum install -y ansible
[root@localhost ~]# vim /etc/ansible/hosts
	[centos7]
	172.18.251.133
	192.168.242.139
	[centos6]
	192.168.242.123

ansible操作其他主机:
必须是在/etc/ansible/hosts这里ip的主机

 [root@localhost app]# echo "hello”> hello
 [root@localhost app]# ansible all -m copy -a  "src=/app/hello dest=/app"
#说明:all 是所有的分组,也就是[centos7]和[centos6]所包含的所有主机  -m  是启用模块,copy是模块名字 -a 是动作 src=/app/hello dest=/app 这个本机为源文件的路径/app/hello    dest=/app为目标主机的路径

验证其他主机是否有这个文件:

[root@localhost app]# cat hello 
hello

ansible对文件的复制及备份:

[root@localhost app]# cat hello 
hello
dd
[root@localhost app]# ansible all -m copy -a  "src=/app/hello dest=/app/ backup=yes"

验证其他主机:

[root@localhost app]# cat hello
hello
dd
[root@localhost app]# cat hello.7118.2018-10-25@10\:14\:23~ 
hello

ansible对文件所属人组权限的修改:
前提:其他主机必须有我们修改的用户和组

[root@localhost app]# ansible all -m copy -a  "src=/app/hello dest=/app/ owner=zhangsan"

[root@localhost app]# ansible all -m copy -a  "src=/app/hello dest=/app/ mode=777"

#说明:以上2条合一合并为一条命令来执行:ansible all -m copy -a  "src=/app/hello dest=/app/ mode=777  owner=zhangsan"

其他主机:

[root@localhost app]# ll
total 8
-rw-r--r-- 1 zhangsan root 9 Oct 25 10:14 hello
-rw-r--r-- 1 root     root 6 Oct 25 10:04 hello.7118.2018-10-25@10:14:23~

[root@localhost app]# ll
total 8
-rwxrwxrwx 1 zhangsan root 9 Oct 25 10:14 hello
-rw-r--r-- 1 root     root 6 Oct 25 10:04 hello.7118.2018-10-25@10:14:23~

利用ansible创建文件:

[root@localhost app]# ansible all -m file -a "path=/app/xin state=touch"  #普通文件的创建
[root@localhost app]# ansible all -m file -a "path=/app/xinxin state=directory" #目录的创建。

其他主机:

[root@localhost app]# ls
hello  hello.7118.2018-10-25@10:14:23~  xin
[root@localhost app]# ls
hello  hello.7118.2018-10-25@10:14:23~  xin  xinxin

利用ansible删除文件或者目录:

[root@localhost app]# ansible all -m file -a "path=/app/xinxin state=absent"

其他主机:

[root@localhost app]# ll
total 8
-rwxrwxrwx 1 zhangsan root 9 Oct 25 10:14 hello
-rw-r--r-- 1 root     root 6 Oct 25 10:04 hello.7118.2018-10-25@10:14:23~
-rw-r--r-- 1 root     root 0 Oct 25 10:29 xin

利用ansible创建软链接:

[root@localhost app]# ansible all -m file -a "path=/app/rlj state=link src=/app/xin"  #xin这个文件一定要事先存在,否则会失败此操作

其他主机:

[root@localhost app]# ll
total 8
-rwxrwxrwx 1 zhangsan root 9 Oct 25 10:14 hello
-rw-r--r-- 1 root     root 6 Oct 25 10:04 hello.7118.2018-10-25@10:14:23~
lrwxrwxrwx 1 root     root 8 Oct 25 10:32 rlj -> /app/xin
-rw-r--r-- 1 root     root 0 Oct 25 10:29 xin

ansible按条件执行:

[root@localhost app]# ansible all -m shell -a "removes=/etc/redhat-release cat /etc/redhat-release" 
#说明:  若removes=/etc/redhat-release存在则执行 下面的 cat /etc/redhat-release  若 removes=/etc/redhat-release不存在则不执行cat的操作    **与其相反的是creates=/etc/redhat-release**

ansible执行脚本:

[root@localhost app]# ansible all -m script -a '/app/sp.sh'
[root@localhost app]# cat sp.sh 
#!/bin/bash
echo hello >/app/xin

其他主机:
脚本执行(将本地的脚本放在all这些主机上去执行) #!/bin/bash 这个必须写上

[root@localhost app]# du -sh xin   #执行脚本前
0	xin
[root@localhost app]# du -sh xin    #执行脚本后
4.0K	xin
[root@localhost app]# cat xin
hello

ansible设置计划任务:

[root@localhost app]# ansible all -m cron -a 'day=*/2 minute=1 hour=10 job="/usr/bin/echo hello" name=mycron'   #name为计划任务的名字

其他主机:

[root@localhost app]# crontab -l
#Ansible: mycron
1 10 */2 * * /usr/bin/echo hello

ansible删除计划任务:

[root@localhost app]# ansible all -m cron -a 'name=mycron state=absent'

其他主机:

[root@localhost app]# crontab -l
[root@localhost app]# 

ansible禁用计划任务:
禁用指定的计划任务,相当于将计划任务注释了

[root@localhost app]# ansible all -m cron -a 'day=*/2 minute=1 hour=10 job="/usr/bin/echo hello" name=mycron disabled=yes'

其他主机:

[root@localhost app]# crontab -l
#Ansible: mycron
#1 10 */2 * * /usr/bin/echo hello

ansible启用禁用的计划任务:

[root@localhost app]# ansible all -m cron -a 'day=*/2 minute=1 hour=10 job="/usr/bin/echo hello" name=mycron disabled=no'

其他主机:

[root@localhost app]# crontab -l
#Ansible: mycron
1 10 */2 * * /usr/bin/echo hello

补充:
1)我们一直都是用的 all 这个分组,意思是所有的分组,不用定义。我们也可单独设置如:
ansible centos7 -m cron -a ‘day=*/2 minute=1 hour=10 job="/usr/bin/echo hello" name=mycron disabled=no’
2)还有一些模块,只是我们不常用就不一一介绍了如:ansible all -m fetch -a ‘src=/var/log/message dest=/root/ansible’ 将远程的文件message拷贝到本地的ansible文件
3)
ansible-doc -l 显示所有的模块
ansible-doc (-s精简版) 模块名 查看模块的帮助
ansible 组名 --list (-l) 列出组中的机器 #组也可以写组中的成员(ip地址)
ansible 组名 -m 模块名 -a ‘命令’ -k
ansible all -u liubei -m common -a ‘id liubei’ -k
说明: all 表示所有组 -u表示以哪个用户的身份执行命令 -m 模块名(例子中的是默认,可以省略)-k 以密码的方式连接ssh
测试:ansible all -a ping
支持通配符
ansible all -a ‘remove=…任意文件 命令’ 若remove=这个存在则执行命令 否则不执行 注释:没有-k是因为我将模式改为秘钥连接ssh了

ansible ip -m shell -a ‘chdir=目录 ./test.sh’ 执行脚本 ip为远程主机的ip 其脚本已经存在了
ansible 组 -m script -a ‘/root/test.sh’ 本地的脚本在远程主机执行

相关内容

    暂无相关文章