首页 > 技术文章 > ansible的简单使用

lulin9501 2019-06-26 22:00 原文

安装ansible

ansible是基于ssh进行工作的,

ansible在epel(第三方社区)源中,安装时要先安装epel
yum install epel       //互联网装这个,或者装阿里的epel源wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install ansible

配置文件:

使用yum安装完,我们可以使用

rpm -ql ansible 查看安装ansible的文件,其中如下两个是我们关心的配置文件

(1)ansible 应用程序的 主配置文件:/etc/ansible/ansible.cfg
(2) Host Inventory 定义管控主机 :/etc/ansible/hosts

配置主机:

配置主机的配置文件,只要管理被控端的主机的:打开后可以看到注释信息,有关hosts文件怎么写,下面对注释信息的解释

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character #号用来注释
#   - Blank lines are ignored 空行可以被忽略
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses 可以输入主机名或者ip地址
#   - A hostname/ip can be a member of multiple groups 一个ip或者主机可以被分在多个组

 

 编辑这个文件/etc/ansible/hosts

/etc/ansible/hosts

配置主机:
Ex1:任何组的头部前面指定,不属于任何组的主机,可以是ip或者是主机名
如:
web201.robin.com
web202.robin.com
192.168.10.201
192.168.10.202


Ex2:一批主机属于一个组,例如定义为 'webservers' 的组如果两个主机的ip是连着的话可以写成192.168.10.20[1:9] 表示从192.168.10.201-209的主机ip
[webserver]
web201.robin.com
web202.robin.com
192.168.10.201
192.168.10.202

 

当ansible管理主机时,可以基于ssh的用户密码验证,也可以基于用户的密钥验证
基于密码验证:基于密码验证的话,在/etc/ansible/hosts 的主机后面加入,主机的信息,

[webserver]
192.168.10.201 ansible_ssh_pass=123456 asible_ssh_user=root   //密码在前,用户在后

以下是Hosts部分中经常用到的变量部分:

ansible_ssh_host     #用于指定被管理的主机的真实IP
ansible_ssh_port     #用于指定连接到被管理主机的ssh端口号,默认是22
ansible_ssh_user     #ssh连接时默认使用的用户名
ansible_ssh_pass     #ssh连接时的密码
ansible_sudo_pass     #使用sudo连接用户时的密码
ansible_sudo_exec     #如果sudo命令不在默认路径,需要指定sudo命令路径
ansible_ssh_private_key_file     #秘钥文件路径,秘钥文件如果不想使用ssh-agent管理时可以使用此选项
ansible_shell_type     #目标系统的shell的类型,默认sh
ansible_connection     #SSH 连接的类型: local , ssh , paramiko,在 ansible 1.2 之前默认是 paramiko ,后来智能选择,优先使用基于 ControlPersist 的 ssh (支持的前提)
ansible_python_interpreter     #用来指定python解释器的路径,默认为/usr/bin/python 同样可以指定ruby 、perl 的路径
ansible_*_interpreter     #其他解释器路径,用法与ansible_python_interpreter类似,这里"*"可以是ruby或才perl等

基于密钥验证:

基于秘钥的方式,不需要在/etc/ansible/hosts配置文件中,将用户名,密码写上了

创建密钥,并且将公钥传输给被管控端
[root@master ~]# ssh-keygen 
[root@master ~]# ssh-copy-id -i 192.168.10.202

 

解决ssh连接慢的问题:

sed  -i "s@#UseDNS yes@UseDNS no@" /etc/ssh/sshd_config 
systemctl restart sshd 重启sshd

配置ansible //基于密钥直接写ip就行

[webserver]
192.168.10.202

ansible模块的使用

ansible命令语法介绍

ansible HOST-PATTERN [-f FORKS] [-M MOD_NAME] [-a MOD_ARGS]
-f FORKS:表示一批处理几台主机,也就是当被管控主机很多时,ansible不是对所有主机同时发起管理操作,而是一批处理几台,然后再换一批,直到所有主机被处理完成,如果不指定,则默认是5台
-m MOD_NAME:指明调用哪个模块执行操作,各个模块所能实现的功能不同,如果不指定,默认是用-m command模块
-a MOD_ARGS:指明使用该模块的执行操作时的参数
-C, --check 不会真正的执行,但是会白跑一次,干跑
--list-hosts 列出匹配到的主机列表
--syntax-check 语法检查
-k, --ask-pass  输入密码

host-pattern 写法 

这个写法,就是ansible命令后面跟的主机或组

- 单个的ip地址
- 多个的ip地址 用,隔开
- 所有的ip地址 all
- 单个的组
- 多个的组
  - 交集
    - 'web:&db'
  - 并集
    - web,db
    - 'web:db'
  - 差集  表示在前面,但是不在后面的
    - 'web:!db' 

查看模块的帮助文档

ansible-doc -l 获取支持的模块

Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-j 以json的方式返回所有的模块信息
-l 列出所有的模块
-s 简短的方式来展示模块信息
[root@bogon ~]# ansible-doc -l|wc -l
2834
ansible-doc ping 查看详细信息

 


modules 具有”幂等”性,意思是如果你再一次地执行 moudle(译者注:比如遇到远端系统被意外改动,需要恢复原状),moudle 只会执行必要的改动,只会改变需要改变的地方.所以重复多次执行 playbook 也很安全.

 

ping模块

1.ping模块 探测助主机是否在线 //先登上客户机 有客户机ping服务器

[root@master ~]# ansible webserver -m ping //webserver 组名 或主机名
192.168.10.201 | SUCCESS => {
"changed": false, 
"ping": "pong"
}
192.168.10.202 | SUCCESS => {
"changed": false, 
"ping": "pong"
}
'

 command模块

command模块 在远程主机执行名,不支持管道,重定向等shell特性不支持这些符号(`"<"', `">"', `"|"', `";"' and `"&"' $)
查看时间信息:

root@master ~]# ansible webserver -m command -a 'date'
192.168.10.201 | SUCCESS | rc=0 >>
2016年 11月 03日 星期四 12:51:20 CST

192.168.10.202 | SUCCESS | rc=0 >>
2016年 11月 03日 星期四 11:51:47 CST

查看负载:
[root@master ~]# ansible webserver -m command -a 'uptime'   //负载情况
192.168.10.201 | SUCCESS | rc=0 >>
 12:52:09 up 1 day, 20:41,  3 users,  load average: 0.00, 0.00, 0.00

192.168.10.202 | SUCCESS | rc=0 >>
 11:52:35 up 21:12,  5 users,  load average: 0.00, 0.00, 0.00

 

 常用参数:不支持管道和重定向 不支持特殊字符,<>|$&!
chdir=   表示指明命令在远程主机上哪个目录下运行,也就是在命令执行前切换到哪个目录下
creates=   当create指定的文件存在时,命令不执行,不存在是则执行 //不会真正创建文件 //仅作判断
removes=   当remove指定的文件存在时执行,不存在时不执行 //不会真正的移除文件 仅作判断
executeble=   指明运行命令的shell程序

[root@master ~]# ansible webserver -m command -a 'chdir=/tmp ls ./' //cd /tmp ls

192.168.10.201 | SUCCESS | rc=0 >>
ansible_mW3Prm

192.168.10.202 | SUCCESS | rc=0 >>
ansible_5mBkhL
keyring-6oyII7
keyring-sjF7FJ
orbit-gdm
orbit-root
passwd
pulse-Ks248rBe0mXP

 


不支持管道和重定向 不支持特殊字符,<>|$&!
[root@master ~]# ansible webserver -m command -a 'echo "hello" > /tmp/aa.txt'

192.168.10.201 | SUCCESS | rc=0 >>
hello > /tmp/aa.txt

192.168.10.202 | SUCCESS | rc=0 >>
hello > /tmp/aa.txt

[root@master ~]# ansible webserver -m command -a 'cat /tmp/aa.txt'
192.168.10.201 | FAILED | rc=1 >>
cat: /tmp/aa.txt: 没有那个文件或目录

192.168.10.202 | FAILED | rc=1 >>
cat: /tmp/aa.txt: 没有那个文件或目录

 


root@master ~]# ansible webserver -a "ls /tmp"
192.168.10.201 | SUCCESS | rc=0 >>
aa.txt
ansible_eZtDY_

192.168.10.202 | SUCCESS | rc=0 >>
aa.txt
ansible_Q4qFAO

[root@master ~]# ansible webserver -m command -a "ls -l chdir=/tmp creates=aa.txt"
[WARNING]: Failure using method (v2_runner_on_ok) in callback plugin
(<ansible.plugins.callback.minimal.CallbackModule object at 0x1f44f10>):
coercing to Unicode: need string or buffer, bool found

[root@master ~]# ansible webserver -m command -a "ls -l chdir=/tmp creates=aaa.txt"
192.168.10.201 | SUCCESS | rc=0 >>
总用量 8
-rw-r--r-- 1 root root 6 11月 3 13:08 aa.txt
drwx------ 2 root root 4096 11月 3 13:19 ansible_xK_rU6

192.168.10.202 | SUCCESS | rc=0 >>
总用量 4
-rw-r--r-- 1 root root 0 11月 3 12:18 aa.txt
drwx------ 2 root root 4096 11月 3 12:20 ansible_LG642m

shell模块  

 shell模块 //和command 一样
在远shell模块程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令
支持shell特性,如管道,重定向等  支持特殊字符,<>|$&!
常见参数有:
        chdir=   表示指明命令在远程主机上哪个目录下运行
        creates=   当create指定的文件存在时,命令不执行,不存在是则执行 //不会真正创建文件 //仅作判断
removes=   当remove指定的文件存在时执行,不存在时不执行 //不会真正的移除文件 仅作判断
        executeble=   指明运行命令的shell程序

 

给用户设置密码echo "1" |passwd --stdin alex 非交互式

 

[root@master ~]# ansible webserver -m shell -a 'echo "hello" > /tmp/aa.txt'
192.168.10.201 | SUCCESS | rc=0 >>

192.168.10.202 | SUCCESS | rc=0 >>

[root@master ~]# ansible webserver -m shell -a 'cat /tmp/aa.txt'
192.168.10.201 | SUCCESS | rc=0 >>
hello
192.168.10.202 | SUCCESS | rc=0 >>
hello

 script模块

在远程上执行本地的脚本

文件必须得有执行权限
ansible web -m script -a "/root/a.sh"
ansible web -m script -a "a.sh"
ansible web -m script -a "chdir=/tmp a.sh" 先切换目录在执行脚本
ansible web -m script -a "creates=/root/a.sh a.sh" 判断的是远程主机上是否存在文件,如果存在就跳过
ansible web -m script -a "removes=/root/a.sh a.sh" 判断远程主机上是否存在,如果存在的话就执行

 

copy模块

.copy模块
拷贝ansible管理端的文件到远程主机的指定位置

常见参数有:
        dest=   指明拷贝文件的目标目录位置,使用绝对路径,如果源是目录,则目标也要是目录,如果目标文件已存在,会覆盖原有内容
        src=   指明本地路径下的某个文件,可以使用相对路径和绝对路径,支持直接指定目录,如果源是目录,则目标也要是目录
        mode=   指明复制时,目标文件的权限
        owner=   指明复制时,目标文件的属主
        group=   指明复制时,目标文件的属组
        content=  指明复制到目标主机上的内容,不能与src一起使用,相当于复制content指明的数据,到目标文件中

  backup 做备份

ansible web -m copy -a "src=a.sh dest=/tmp/a.sh" 复制文件  根据md5sum来计算 如果MD5值一样,则不复制文件
ansible web
-m copy -a "src=a.sh dest=/tmp/a.sh backup=yes owner=alex mode=755" 复制文件,并修改文件的属主 权限,并备份文件,如果之前存在同名的文件,则备份,如果不存在,则不备份,这个文件和之前的文件的内容不一样,会备份 ansible web -m copy -a "dest=/tmp src=/etc/init.d" 复制整个文件夹 ansible web -m copy -a "dest=/tmp src=/etc/init.d/" 复制文件夹下面的所有文件 ansible web -m copy -a "dest=/tmp src=/etc/init.d owner=alex" 复制整个目录,并修改属主,如果目录改变了,则文件也会跟着发生改变 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/b.txt owner=alex mode=777" 直接往文件里面写内容,覆盖写,要慎用

 

 

[root@master ~]# ansible webserver -m copy -a 'src=install.log dest=/tmp mode=777 owner=nobody group=root'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "checksum": "b72d2498186ae6811dc9ccbca2260c55d62ed1fc", 
    "dest": "/tmp/install.log", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "650b10045f42b322e01d3932642c602f", 
    "mode": "0777", 
    "owner": "nobody", 
    "size": 45941, 
    "src": "/root/.ansible/tmp/ansible-tmp-1478471537.18-168088637164915/source", 
    "state": "file", 
    "uid": 99
}
192.168.10.202 | SUCCESS => {
    "changed": false, 
    "checksum": "b72d2498186ae6811dc9ccbca2260c55d62ed1fc", 
    "dest": "/tmp/install.log", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "nobody", 
    "path": "/tmp/install.log", 
    "size": 45941, 
    "state": "file", 
    "uid": 99
}
[root@master ~]# ansible webserver -m copy -a 'content="hello world" dest=/tmp/test.txt mode=777'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", 
    "dest": "/tmp/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "5eb63bbbe01eeed093cb22bb8f5acdc3", 
    "mode": "0777", 
    "owner": "root", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1478471640.54-71154790406248/source", 
    "state": "file", 
    "uid": 0
}
192.168.10.202 | SUCCESS => {
    "changed": false, 
    "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", 
    "dest": "/tmp/test.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "path": "/tmp/test.txt", 
    "size": 11, 
    "state": "file", 
    "uid": 0
}

cron模块

管理计划任务的模块
        常见参数有:
        minute=  指明计划任务的分钟,支持格式:0-59,*,*/2等,与正常cron任务定义的一样的语法,省略时,默认为*,也就是每分钟都执行
        hour=  指明计划任务的小时,支持的语法:0-23,*,*/2等,省略时,默认为*,也就是每小时都执行
        day=  指明计划任务的天,支持的语法:1-31,*,*/2等,省略时,默认为*,也就是每天都执行
        month=  指明计划任务的月,支持的语法为:1-12,*,*/2等,省略时,默认为*,也就是每月都执行
        weekday=  指明计划任务的星期几,支持的语法为:0-6,*等,省略时,默认为*,也就是每星期几都执行
        reboot  指明计划任务执行的时间为每次重启之后
        name=   给该计划任务取个名称,必须要给明。每个任务的名称不能一样。删除任务时,只需要给明任务的名称即可
        job=  执行的任务是什么,当state=present时才有意义
        state=present|absent   表示这个任务是创建还是删除,present表示创建,absent表示删除,默认是present

  disabled 禁用

ansible web -m cron -a "minute=3 name=touchfile2 job='touch /tmp/alex.txt'" 创建计划任务  名字是不可以重复的,名字如果不指定的话,就是None
ansible web -m cron -a "name=touchfile state=absent" 删除 根据name来删除, 只要重名就都会被删除
ansible web -m cron -a "minute=3 name=touchfile job='touch /tmp/alex.txt' disabled=yes" 注释掉计划任务,表现形式是#
disabled 必须要有job才可以使用
ansible web -m cron -a "minute=4 name=touchfile job='touch /tmp/alex.txt' disabled=yes"  同名如果说时间不一样的话,则会被修改

 

 

[root@master ~]# ansible webserver -m cron -a 'minute=10 hour=10-20 day=10 name="test cron" job="ntpdate 192.168.10.200 &> /dev/null"'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}
192.168.10.202 | SUCCESS => {
    "changed": false, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}

[root@master ~]# ansible webserver -m shell -a "crontab -l"
192.168.10.201 | SUCCESS | rc=0 >>
#Ansible: test cron
10 10-20 10 * * ntpdate 192.168.10.200 &> /dev/null

192.168.10.202 | SUCCESS | rc=0 >>
#Ansible: test cron
10 10-20 10 * * ntpdate 192.168.10.200 &> /dev/null

[root@master ~]# ansible webserver -m cron -a 'minute=10 hour=10-20 day=10 name="test" job="ntpdate 192.168.10.200 &> /dev/null" state=absent'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@master ~]# ansible webserver -m shell -a "crontab -l"
192.168.10.201 | SUCCESS | rc=0 >>


192.168.10.202 | SUCCESS | rc=0 >>

fetch模块

6.fetch模块 //将路径取下来包括文件名

以远程主机的ip地址或者主机名创建目录,并且保留了原来的目录结构
从远程主机拉取文件到本地
        一般情况下,只会从一个远程节点拉取数据
        常见参数有:
        dest=  从远程主机上拉取的文件存放在本地的位置,一般只能是目录
        src=   指明远程主机上要拉取的文件,只能是文件,不能是目录
服务器 客户机

[root@master ~]# ansible 192.168.10.202 -m fetch -a 'src=/tmp/test.txt dest=/tmp'
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", 
    "dest": "/tmp/192.168.10.202/tmp/test.txt", 
    "md5sum": "5eb63bbbe01eeed093cb22bb8f5acdc3", 
    "remote_checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", 
    "remote_md5sum": null
}

[root@master ~]# ansible webserver -m fetch -a 'src=/tmp/test.txt dest=/tmp  //从多个客户机下载

[root@master ~]# ls /tmp/
192.168.10.202
[root@master ~]# cat /tmp/192.168.10.202/tmp/test.txt

file模块

7.file模块
用于设定远程主机上的文件属性
        常见参数有:
        path=   指明对哪个文件修改其属性
        src=   指明path=指明的文件是软链接文件,其对应的源文件是谁,必须要在state=link时才有用
        state=directory|link|absent   表示创建的文件是目录还是软链接
        owner=   指明文件的属主
        group=   指明文件的属组
        mode=   指明文件的权限

        创建软链接的用法:
            src=  path=  state=link
        修改文件属性的用法:
            path=  owner=  mode=  group=
        创建目录的用法:
            path=  state=directory
        删除文件:
            path= state=absent

state absent 删除 directory 目录 file 如果不存在也不创建 touch 创建文件 
ansible web -m file -a "path=/root/file.txt state=touch" 创建文件
ansible web -m file -a "path=/root/dir state=directory" 创建目录
ansible web -m file -a "path=/root/f src=/root/file.txt owner=alex state=link" 创建软连接并且修改属主 
ansible web -m file -a "path=/root/f state=absent" 删除,可以删除任何的目录或者文件

 

补充

软连接 相当于windows的快捷方式 ln -s 软连接文件大小是源文件文件名的字符个数 源文件发生改变 链接文件也跟着变化 当源文件被删除以后,软连接就不可以使用

硬链接 ln 做硬链接 源文件发生改变 硬链接也会发生改变 不可以对目录做 不可以跨越分区 当源文件被删除以后,硬链接可以继续使用

复制 源文件发生改变,目录文件不会发生改变,源文件删除,目标文件没有影响

日志收集 收集日志

 

创建软连接
[root@master ~]# ansible webserver -m file -a 'src=/tmp/test.txt path=/home/test.link state=link'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "dest": "/home/test.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 13, 
    "src": "/tmp/test.txt", 
    "state": "link", 
    "uid": 0
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "dest": "/home/test.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 13, 
    "src": "/tmp/test.txt", 
    "state": "link", 
    "uid": 0
}
[root@master ~]# ansible webserver -m shell -a 'ls -l /home/test.link'
192.168.10.201 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 root root 13 11月  3 13:45 /home/test.link -> /tmp/test.txt

192.168.10.202 | SUCCESS | rc=0 >>
lrwxrwxrwx 1 root root 13 11月  3 12:45 /home/test.link -> /tmp/test.txt

删除文件
[root@master ~]# ansible webserver -m file -a 'path=/home/test.link state=absent'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "path": "/home/test.link", 
    "state": "absent"
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "path": "/home/test.link", 
    "state": "absent"
}





修改文件属性
[root@master ~]# ansible webserver -m file -a 'path=/tmp/test.txt owner=nobody group=nobody mode=000'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "gid": 99, 
    "group": "nobody", 
    "mode": "0", 
    "owner": "nobody", 
    "path": "/tmp/test.txt", 
    "size": 11, 
    "state": "file", 
    "uid": 99
}
192.168.10.202 | SUCCESS => {
    "changed": false, 
    "gid": 99, 
    "group": "nobody", 
    "mode": "0", 
    "owner": "nobody", 
    "path": "/tmp/test.txt", 
    "size": 11, 
    "state": "file", 
    "uid": 99
}
[root@master ~]# ansible webserver -m shell -a 'ls -l /tmp/test.txt'
192.168.10.201 | SUCCESS | rc=0 >>
---------- 1 nobody nobody 11 11月  3 13:24 /tmp/test.txt

192.168.10.202 | SUCCESS | rc=0 >>
---------- 1 nobody nobody 11 11月  3 12:24 /tmp/test.txt

创建目录
[root@master ~]# ansible webserver -m file -a 'path=/tmp/dir state=directory'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/dir", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
[root@master ~]# ansible webserver -m shell -a 'ls -ld /tmp/dir'
192.168.10.201 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 4096 11月  3 13:48 /tmp/dir

192.168.10.202 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 4096 11月  3 12:49 /tmp/dir

删除目录
[root@master ~]# ansible webserver -m file -a 'path=/tmp/dir state=absent'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/dir", 
    "state": "absent"
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/dir", 
    "state": "absent"
}


[root@master ~]# ansible webserver -m shell -a 'ls -l /tmp/'
192.168.10.201 | SUCCESS | rc=0 >>
总用量 60
-rw-r--r-- 1 root   root       6 11月  3 13:08 aa.txt
drwx------ 2 root   root    4096 11月  3 13:49 ansible_o9JN6a
-rwxrwxrwx 1 nobody root   45941 11月  3 13:22 install.log
---------- 1 nobody nobody    11 11月  3 13:24 test.txt

192.168.10.202 | SUCCESS | rc=0 >>
总用量 56
-rw-r--r-- 1 root   root       0 11月  3 12:18 aa.txt
drwx------ 2 root   root    4096 11月  3 12:50 ansible_3gtB6P
-rwxrwxrwx 1 nobody root   45941 11月  3 12:22 install.log
---------- 1 nobody nobody    11 11月  3 12:24 test.txt

8.hostname模块  //l临时修改
        管理远程主机上的主机名
        常用参数有
        name=  指明主机名

[root@master ~]# ansible webserver -m shell -a 'hostname'
192.168.10.201 | SUCCESS | rc=0 >>
agent201.puppet.com

192.168.10.202 | SUCCESS | rc=0 >>
agent202.puppet.com

[root@master ~]# ansible 192.168.10.201 -m hostname -a 'name=agent201.ansible.com'
192.168.10.201 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "ansible.com", 
        "ansible_fqdn": "agent201.ansible.com", 
        "ansible_hostname": "agent201", 
        "ansible_nodename": "agent201.ansible.com"
    }, 
    "changed": true, 
    "name": "agent201.ansible.com"
}
[root@master ~]# ansible webserver -m shell -a 'hostname'
192.168.10.201 | SUCCESS | rc=0 >>
agent201.ansible.com

192.168.10.202 | SUCCESS | rc=0 >>
agent202.puppet.com

yum模块

基于yum机制,对远程主机管理程序包
        常用参数有:
        name=   指明程序包的名称,可以带上版本号,不指明版本,就是默认最新版本。
        state=present|latest|absent   指明对程序包执行的操作,present表示安装程序包,latest表示安装最新版本的程序包,absent表示卸载程序包
        disablerepo=    在用yum安装时,临时禁用某个仓库,仓库的ID
        enablerepo=    在用yum安装时,临时启用某个仓库,仓库的ID
        conf_file=   指明yum运行时采用哪个配置文件,而不是使用默认的配置文件
        diable_gpg_check=yes|no  是否启用gpg-check

卸载软件包:
[root@master ~]# ansible webserver -m yum -a 'name=nmap state=absent'

安装软件包:
[root@master ~]# ansible webserver -m yum -a 'name=nmap state=present'

查询:
[root@master ~]# ansible webserver -m shell -a 'rpm -q nmap'
192.168.10.201 | SUCCESS | rc=0 >>
nmap-5.51-4.el6.x86_64

192.168.10.202 | SUCCESS | rc=0 >>
nmap-5.51-4.el6.x86_64

[root@master ~]# ansible webserver -m yum -a 'name=httpd disable_gpg_check=yes disablerepo=yum state=present'

service模块

用来管理远程主机上的服务的模块
        常见参数有:
        name=   被管理的服务名称
        state=started|stopped|restarted |reloaded  表示启动或关闭或重启
        enabled=yes|no  表示要不要设定该服务开机自启动
        runlevel=   如果设定了enabled开机自动启动,则要定义在哪些运行级别下自动启动

  use 启动服务的用户

ansible web -m service -a "name=nginx state=started" 启动服务
ansible web -m service -a "name=nginx state=stopped" 关闭服务
ansible web -m service -a "name=nginx state=started enabled=yes" 启动服务并将服务设置成开机自启动

 

[root@master ~]# ansible webserver -m service -a 'name=httpd state=started'
192.168.10.201 | SUCCESS => {
    "changed": false, 
    "name": "httpd", 
    "state": "started"
}
192.168.10.202 | SUCCESS => {
    "changed": false, 
    "name": "httpd", 
    "state": "started"
}

[root@master ~]# ansible webserver -m shell -a 'netstat -anplt |grep :80'
192.168.10.201 | SUCCESS | rc=0 >>
tcp        0      0 192.168.10.201:37650        111.108.54.42:80            ESTABLISHED 2307/clock-applet   
tcp        0      0 :::80                       :::*                        LISTEN      12385/httpd         

192.168.10.202 | SUCCESS | rc=0 >>
tcp        0      0 :::80                       :::*                        LISTEN      37876/httpd   


[root@master ~]# ansible webserver -m service -a 'name=httpd state=stopped'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "stopped"
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "stopped"
}

[root@master ~]# ansible webserver -m service -a 'name=httpd state=restarted enabled=yes runlevel=2345'
[root@master ~]# ansible webserver -m shell -a 'chkconfig --list | grep httpd'

uri模块

11. uri模块
        如果远端是web服务器,可以利用ansible直接请求某个网页

        常见参数有:
        url=  指明请求的url的路径,如:http://10.1.32.68/test.jpg
        user=  如果请求的url需要认nstall.lo证,则认证的用户名是什么
        password=  如果请求的url需要认证,则认证的密码是什么
        method=  指明请求的方法,如GET、POST…
        body=   指明报文中实体部分的内容,一般是POST方法或PUT方法时用到
        HEADER_   自定义请求报文中的添加的首部

[root@master ~]# ansible webserver -m uri -a 'url=http://192.168.10.201/test.html'

group模块

用来添加或删除远端主机的用户组
        常见参数有:
        name=   被管理的组名
        state=present|absent   是添加还是删除,不指名默认为添加
        gid=   指明GID
        system=yes|no   是否为系统组

[root@master ~]# ansible webserver -m group -a 'name=test gid=2000 system=yes'

[root@master ~]# ansible webserver -m shell -a 'tail -1 /etc/group'

[root@master ~]# ansible webserver -m group -a 'name=test state=absent'

user模块

管理远程主机上的用户的账号
        常见参数有:
        name=   指明要管理的账号名称
        state=present|absent   指明是创建账号还是删除账号,present表示创建,absent表示删除
        system=yes|no   指明是否为系统账号
        uid=   指明用户UID
        group=   指明用户的基本组
        groups=   指明用户的附加组
        shell=   指明默认的shell
        home=   指明用户的家目录
        move_home=yes|no   当home设定了家目录,如果要创建的家目录已存在,是否将已存在的家目录进行移动
        password=   指明用户的密码,最好使用加密好的字符串
        comment=   指明用户的注释信息
        remove=yes|no   当state=absent时,也就是删除用户时,是否要删除用户的而家目录

ansible web -m user -a "name=alex21 uid=2001 home=/opt/alex21 shell=/sbin/nologin" 创建用户,并制定用户的id,家目录,登录后的shell
ansible web -m user -a "name=alex22 groups=root,alex21" 创建用户,并制定用户的附加组
ansible web -m user -a "name=alex22 state=absent" 删除用户
ansible web -m user -a "name=alex21 state=absent remove=yes"  删除用户并且删除用户的家目录
ansible web -m user -a "name=alex23 system=yes"  创建系统用户

 

 

 

[root@master ~]# ansible webserver -m user -a 'name=robin system=no uid=2000 group=test groups=root shell=/sbin/bash home=/home/robin password=123 comment=test'
[root@master ~]# ansible webserver -m shell -a 'id robin'
192.168.10.201 | SUCCESS | rc=0 >>
uid=2000(robin) gid=2000(test) 组=2000(test),0(root)

192.168.10.202 | SUCCESS | rc=0 >>


[root@master ~]# ansible webserver -m user -a 'name=robin remove=yes state=absent' remove 删除家目录

 script模块

 将管理端的某个脚本,移动到远端nstall.lo主机(不需要指明传递到远端主机的哪个路径下,系统会自动移动,然后执行),
 一般是自动移动到远端主机的/root/.ansible/tmp目录下,然后自动给予其权限,然后再开个子shell然后运行脚本,运行完成后删除脚本

测试脚本
[root@master ~]# cat test.sh 
#!/bin/bash
echo "hello world" >> /tmp/robin.txt

[root@master ~]# ansible webserver -m script -a '/root/test.sh'
192.168.10.201 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}
192.168.10.202 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}
[root@master ~]# ansible webserver -m shell -a 'ls /tmp/robin.txt'
192.168.10.201 | SUCCESS | rc=0 >>
/tmp/robin.txt

192.168.10.202 | SUCCESS | rnstall.loc=0 >>
/tmp/robin.txt

 

 setup模块

可收集远程主机的facts变量的信息,相当于收集了目标主机的相关信息(如内核版本、操作系统信息、cpu、…),保存在ansible的内置变量中,之后我们有需要用到时,直接调用变量即可

 

[root@master ~]# ansible webserver -m setup

ansible_all_ipv4_addresses 192.168.10.202

.template模块

16.template模块
 基于模板方式,生成一个模板文件,复制到远程主机,让远程主机基于模板,生成符合远程主机自身的文件

        注意:此模块不能在命令行使用,只能用在playbook中

        常见的参数有:
        src=  指明管理端本地的模板文件的目录
        dest=   指明将模板文件拷贝到远程主机的哪个目录下
        owner=  指明拷贝到远程主机的文件的属主
        group=  指明拷贝到远程主机的文件的属组
        mode=   指明拷贝到远程主机的文件的权限

 

[root@master ~]# cat temp.txt 
this is {{ ansible_hostname  }}    

[root@master ~]# cat test.yml 
- hosts: 192.168.10.202
  remote_user: root
  tasks:
  - name: test template 
    template: src=/root/temp.txt dest=/tmp

[root@master ~]# ansible-playbook test.yml 

PLAY [192.168.10.202] **********************************************************

TASK [setup] *******************************************************************
ok: [192.168.10.202]

TASK [test template module] ****************************************************
changed: [192.168.10.202]

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

[root@master ~]# ansible 192.168.10.202 -m shell -a 'cat /tmp/temp.txt'
192.168.10.202 | SUCCESS | rc=0 >>
this is agent202

 pip

补充

pip freeze > a.txt  导出安装的第三方包
pip list 查看安装的第三方包
pip install -r a.txt  安装

requirements 指定文件name 第三方包的名称virtualenv 虚拟环境ansible web -m pip -a "name=django==1.11.11"  安装包

 

推荐阅读