ansible初识
运维工作
系统安装:
bare metal(实体机 ):pxe技术,cobbler技术
virtural machine(虚拟机):VMware,批量部署操作
应用程序部署&configuration工具:
puppet:ruby
saltstack:python
chef
cfengine
command & control工具:
fabric:python
func
程序发布:
人工智能(手动发布)
shell脚本
发布程序(运维程序)
发布要求:
1.不影响用户体验
2.系统不能停机
3.不能够导致系统故障或者造成系统完全不可用
程序的发布要基于灰度模型:
基于主机
基于用户
灰度发布机制:
发布路径:/webapps/tuangou----->/webapps/tuangou1.0------>/webapps/tuangou1.1......
在调度器上下线一批机器(标记为维护模式)--->关闭服务---->部署新版本---->启动服务----->在调度器上启用这一批机器
监控系统:
zabbix
运维工具的分类:
agent:需要安装代理程序;例如
无agent: 启动ssh连接管理向服务端发送请求,危险性较高,例如ansible,fabric
ansible:
足够轻量化,
能够提供configuration&command control基本功能
可以实现的功能:
实现配置功能
部署功能
版本控制
任务编排
特性:
模块化:高度模块化,完成特定的任务
基于Python语言实现,由paramiko,pyYAML和jinjia2三个关键组成
部署简单:agentless
支持自定义模块
支持playbook
幂等性:重复执行和一次执行的效果一致
组成部分:
ansible实战:
安装:
[root@bogon ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm #安装epel源
Retrieving http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
warning: /var/tmp/rpm-tmp.6BsPNz: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:epel-release-7-9 ################################# [100%]
[root@bogon ~]# yum install ansible -y #yum 安装ansible
有用的配置文件:
[root@bogon ~]# rpm -ql ansible |head
/etc/ansible
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #host 主机列表
/etc/ansible/roles #角色列表
/usr/bin/ansible
/usr/bin/ansible-console
/usr/bin/ansible-doc #查看文档
/usr/bin/ansible-galaxy
/usr/bin/ansible-playbook
/usr/bin/ansible-pull
配置管理主机的host文件:
[root@bogon ansible]# vim /etc/ansible/hosts
# Ex 1: Ungrouped hosts, specify before any group headers. #单个主机方式管理
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group #分组方式管理
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# Ex 3: A collection of database servers in the 'dbservers' group #正则方式定义 ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57
根据主控端生成ssh密钥文件:
[root@bogon ansible]# ssh-keygen -t rsa -P '' #空密码密钥文件的生成
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d7:dc:43:43:cb:ad:7b:7a:95:78:cb:b3:2c:fb:67:56 root@bogon
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| o o |
| = .|
| o o o |
| S . o = .|
| . . =E|
| + =|
| ..*=|
| .=B+|
+-----------------+
添加信任到客户端:
[root@bogon ansible]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.1.1.11 发送公钥文件到客户端
The authenticity of host '172.1.1.11 (172.1.1.11)' can't be established.
ECDSA key fingerprint is 48:6c:db:8c:c7:90:ec:3e:e6:ee:13:ae:cc:cb:a8:7b.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.1.1.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.1.1.11'"
and check to make sure that only the key(s) you wanted were added.
第一个测试命令:
[root@bogon ansible]# ansible all -m ping #对所有分组机器执行ping测试
172.1.1.11 | SUCCESS => { #返回响应 "changed": false, "ping": "pong" } 172.1.1.10 | SUCCESS => { "changed": false, "ping": "pong" }
简单实用格式:
ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS
ansible进阶
ansible配置文件介绍:
[root@bogon ~]# vim /etc/ansible/ansible.cfg 打开配置文件
[root@bogon ~]# egrep -v "^#|^$" /etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
remote_tmp = ~/.ansible/tmp
local_tmp = ~/.ansible/tmp
forks = 5
sudo_user = root
transport = smart
module_lang = C
gathering = implicit
gather_timeout = 10
sudo_exe = sudo
- ansible的简单实用格式:
ansible HOST-PATTERN -m MODULE_NAME -a MODULE_ARGS
ansible的常用模块介绍
- 查看ansible模块的命令:ansible-doc-l
[root@bogon ~]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_service_group Manage A10 Networks devices' service groups
a10_virtual_server Manage A10 Networks devices' virtual servers
acl Sets and retrieves file ACL information.
add_host add a host (and alternatively a group) to the ansible-playbook in-memory inventory
airbrake_deployment Notify airbrake about app deployments
alternatives Manages alternative programs for common commands
apache2_mod_proxy Set and/or get members' attributes of an Apache httpd 2.4 mod_proxy balancer pool
apache2_module enables/disables a module of the Apache2 webserver
apk Manages apk packages
还有很多。。。。。。。
- ansible命令行调用模块的语法如下:
ansible 操作目标 -m 模块名称 -a 模块参数
- 下面是常用的模块:
- hostname模块:指定主机名
- known_hosts
- command模块:用于执行命令
实战:给所有主机添加user1用户,并给添加user1当做密码
[root@bogon ~]# ansible webservers -m command -a "useradd user1"
172.1.1.7 | SUCCESS | rc=0 >>
- 172.1.1.12 | SUCCESS | rc=0 >>
- [root@bogon ~]# ansible webservers -m shell -a "echo user1|passwd --stdin user1"
172.1.1.7 | SUCCESS | rc=0 >>
user1|passwd --stdin user1
- 172.1.1.12 | SUCCESS | rc=0 >>
user1|passwd --stdin user1
- setup模块:可以获取ansible客户端详细信息
[root@bogon ~]# ansible 172.1.1.7 -m setup
172.1.1.7 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.1.1.7",
"10.70.12.76"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe18:9f1c",
"fe80::20c:29ff:fe18:9f26"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/02/2015",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"KEYBOARDTYPE": "pc",
"KEYTABLE": "us",
"LANG": "en_US.UTF-8",
"SYSFONT": "latarcyrheb-sun16",
"quiet": true,
"rd_LVM_LV": "vg_centos6/lv_root",
"rd_NO_DM": true,
"rd_NO_LUKS": true,
。。。。。太多了省略一部分
- copy模块:该模块可以实现从ansible的主控端向被控端传送文件的功能,类似于scp。记得关闭所有机器的selinux,否则会出错
[root@bogon ~]# ansible webservers -m copy -a "src=/opt/test.py dest=/opt/ owner=root group=root mode=0755 force=yes"
172.1.1.7 | SUCCESS => { #成功标识
"changed": true,
"checksum": "acfd655fda04d0ace7c2fefb627455939df6ea1d",
"dest": "/opt/test.py",
"gid": 0,
"group": "root",
"md5sum": "d7627cf18914922953c203ee8dd20e14",
"mode": "0755",
"owner": "root",
"size": 99,
"src": "/root/.ansible/tmp/ansible-tmp-1487355248.58-122971534024237/source",
"state": "file",
"uid": 0
}
172.1.1.12 | SUCCESS => {
"changed": true,
"checksum": "acfd655fda04d0ace7c2fefb627455939df6ea1d",
"dest": "/opt/test.py",
"gid": 0,
"group": "root",
"md5sum": "d7627cf18914922953c203ee8dd20e14",
"mode": "0755",
"owner": "root",
"size": 99,
"src": "/root/.ansible/tmp/ansible-tmp-1487355249.12-32499945891444/source",
"state": "file",
"uid": 0
}
[root@bogon ~]# ansible webservers -m copy -a "content='hello word' dest=/opt/test"
172.1.1.7 | SUCCESS => {
"changed": true,
"checksum": "e0738b87e67bbfc9c5b77556665064446430e81c",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "13574ef0d58b50fab38ec841efe39df4",
"mode": "0644",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1487508106.35-273692325355048/source",
"state": "file",
"uid": 0
}
172.1.1.12 | SUCCESS => {
"changed": true,
"checksum": "e0738b87e67bbfc9c5b77556665064446430e81c",
"dest": "/opt/test",
"gid": 0,
"group": "root",
"md5sum": "13574ef0d58b50fab38ec841efe39df4",
"mode": "0644",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1487508106.84-111192117940766/source",
"state": "file",
"uid": 0
}
如果报错的话就在主控端执行:
[root@bogon ~]# ansible webservers -m command -a "yum install libselinux-python -y"
命令分析:ansible webservers-m copy -a"src=/opt/test.py dest=/opt/ owner=root group=root mode=0755 force=yes"
-m:指定模块
-a:指定参数
src:原文件位置
dest:目标位置
owner:属主
group:属组
mode:权限
force:如果目标主机包含此文件,但内容不同则设置yes会强制覆盖,设置为no的时候只有当目标主机不包含此文件时候才会复制该文件到目标主机
backup:在覆盖之前保留源文件,备份源文件
复制整个目录里的文件就不需要再src目录之后加/
- sychronize模块:使用sychronize模块会调用rsync命令首先要记得安装好rsync软件包
[root@bogon ~]# ansible webservers -m command -a "yum install rsync -y"
172.1.1.12 | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.yun-idc.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
Package rsync-3.0.9-17.el7.x86_64 already installed and latest version
Nothing to do
172.1.1.7 | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* epel: mirrors.tuna.tsinghua.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.6-9.el6_4.1
sychronize模块是将ansible主控机器的指定目录push到客户端的指定目录操作如下:
[root@bogon opt]# ansible 172.1.1.12 -m synchronize -a "src=/opt/scripts/ dest=/opt/scripts/ delete=yes compress=yes"
172.1.1.12 | SUCCESS => {
"changed": true,
"cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"/opt/scripts/\" \"172.1.1.12:/opt/scripts/\"",
"msg": ".d..t...... ./\n<f+++++++++ anaconda-ks.cfg\n<f+++++++++ test.py\n<f+++++++++ test.yaml\n<f+++++++++ test2.py\n<f+++++++++ test2.yaml\n<f+++++++++ test3.py\n<f+++++++++ test3.yaml\n<f+++++++++ test4.py\n<f+++++++++ test4.yaml\n",
"rc": 0,
"stdout_lines": [
".d..t...... ./",
"<f+++++++++ anaconda-ks.cfg",
"<f+++++++++ test.py",
"<f+++++++++ test.yaml",
"<f+++++++++ test2.py",
"<f+++++++++ test2.yaml",
"<f+++++++++ test3.py",
"<f+++++++++ test3.yaml",
"<f+++++++++ test4.py",
"<f+++++++++ test4.yaml"
]
}
ansible172.1.1.12-m synchronize -a"src=/opt/scripts/ dest=/opt/scripts/ delete=yes compress=yes"
其中:delete实现自动对比删除文件实现对文件同步的一致性,compress是压缩传输的意思
- file模块:主要用来设置文件或者目录的属性
- group:定义属组
- mode:定义权限
- owner:定义属主
- path:定义文件或者目录的路径
- recurse:递归设置目录的属性
- src:被链接的原文件位置
- dest:被链接的目标位置
- force:强制创建软连接,有yes和no两个选项
- state:后面连接文件的各种状态,如下:
- link:创建软连接
- hard:创建硬连接
- directory:如果目录不存在,就创建目录
- file:即使文件不存在,也不会被创建
- absent:删除目录、文件、或者链接文件
- touch:如果文件不存在则会创建一个新的文件,如果文件或者目录存在就会更新最后的修改时间
实例一: 将客户机的172.1.1.12的/opt/scripts/test.py 软连接到/tmp/test.py下,查看并删除:
[root@bogon opt]# ansible 172.1.1.12 -m file -a "src=/opt/scripts/test.py dest=/tmp/test.py state=link" #创建软连接
172.1.1.12 | SUCCESS => {
"changed": true,
"dest": "/tmp/test.py",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 20,
"src": "/opt/scripts/test.py",
"state": "link",
"uid": 0
}
[root@bogon opt]# ansible 172.1.1.12 -m command -a "ls -l /tmp" #查看连接时否存在
172.1.1.12 | SUCCESS | rc=0 >>
total 4
drwx------ 2 root root 63 Feb 18 14:18 ansible_VlEoeC
drwxr-xr-x 2 root root 6 Feb 18 14:16 cfp
-rwx------. 1 root root 827 Feb 15 17:16 ks-script-c9emev
lrwxrwxrwx 1 root root 20 Feb 18 14:15 test.py -> /opt/scripts/test.py
-rw-------. 1 root root 0 Feb 15 17:00 yum.log
[root@bogon opt]# ansible 172.1.1.12 -m file -a "src=/opt/scripts/test.py dest=/tmp/test.py state=absent" #删除软连接
172.1.1.12 | SUCCESS => {
"changed": true,
"path": "/tmp/test.py",
"state": "absent"
}
实例二:在所有的被控端的机器上创建/test.txt文件属主和数组均为root,权限为0755,然后查看和删除
[root@bogon opt]# ansible all -m file -a "path=/test.txt group=root owner=root state=touch mode=0755" #创建test.txt文件
172.1.1.12 | SUCCESS => {
"changed": true,
"dest": "/test.txt",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
172.1.1.7 | SUCCESS => {
"changed": true,
"dest": "/test.txt",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
[root@bogon opt]# ansible all -m command -a "ls -l /test.txt " #查看文件
172.1.1.12 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 0 Feb 18 14:24 /test.txt
172.1.1.7 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 root root 0 Jan 17 05:23 /test.txt
[root@bogon opt]# ansible all -m file -a "path=/test.txt group=root owner=root state=absent mode=0755" #删除文件
172.1.1.7 | SUCCESS => {
"changed": true,
"path": "/test.txt",
"state": "absent"
}
172.1.1.12 | SUCCESS => {
"changed": true,
"path": "/test.txt",
"state": "absent"
}
实例三:在webservers创建/test目录,属主和属组均为root,权限为0755,然后查看并删除
[root@bogon opt]# ansible webservers -m file -a "path=/test group=root owner=root mode=0755 state=directory" #创建目录
172.1.1.7 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/test",
"size": 4096,
"state": "directory",
"uid": 0
}
172.1.1.12 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/test",
"size": 6,
"state": "directory",
"uid": 0
}
[root@bogon opt]# ansible webservers -m command -a "ls -l /test" #查看目录
172.1.1.7 | SUCCESS | rc=0 >>
total 0
172.1.1.12 | SUCCESS | rc=0 >>
total 0
[root@bogon opt]# ansible webservers -m file -a "path=/test state=absent" #删除目录
172.1.1.7 | SUCCESS => {
"changed": true,
"path": "/test",
"state": "absent"
}
172.1.1.12 | SUCCESS => {
"changed": true,
"path": "/test",
"state": "absent"
}
- ping模块 :测试是否可以ping
[root@bogon opt]# ansible all -m ping
172.1.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.1.1.12 | SUCCESS => {
"changed": false,
"ping": "pong"
}
- group模块:可以在创建节点上创建自己定义的组
实例:在webservers组建立test组定义gid=666,然后再查看和删除:
[root@bogon opt]# ansible webservers -m group -a "gid=666 name=test" #创建组
172.1.1.7 | SUCCESS => {
"changed": true,
"gid": 666,
"name": "test",
"state": "present",
"system": false
}
172.1.1.12 | SUCCESS => {
"changed": true,
"gid": 666,
"name": "test",
"state": "present",
"system": false
}
[root@bogon opt]# ansible webservers -m shell -a "cat /etc/group|grep test" #查看,此处用shell是因为command不支持管道
172.1.1.7 | SUCCESS | rc=0 >>
test:x:666:
172.1.1.12 | SUCCESS | rc=0 >>
test:x:666:、
[root@bogon opt]# ansible all -m group -a "name=test gid=666 state=absent" #删除组
172.1.1.7 | SUCCESS => {
"changed": false,
"name": "test",
"state": "absent"
}
172.1.1.12 | SUCCESS => {
"changed": false,
"name": "test",
"state": "absent"
}
-
- user模块:系统用户的创建
实例:在所有的机器上创建test用户,然后再将其加入test组,最后查看组合用户状态
[root@bogon opt]# ansible all -m user -a "name=test group=test" #创建用户并添加到组
172.1.1.7 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 666,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 503
}
172.1.1.12 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 666,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
[root@bogon opt]# ansible all -m shell -a "cat /etc/passwd|grep test" #检查存在性
172.1.1.7 | SUCCESS | rc=0 >>
test:x:503:666::/home/test:/bin/bash
172.1.1.12 | SUCCESS | rc=0 >>
test:x:1000:666::/home/test:/bin/bash
[root@bogon opt]# ansible all -m user -a "name=test state=absent remove=yes" #删除用户
172.1.1.7 | SUCCESS => {
"changed": true,
"force": false,
"name": "test",
"remove": true,
"state": "absent"
}
172.1.1.12 | SUCCESS => {
"changed": true,
"force": false,
"name": "test",
"remove": true,
"state": "absent"
}
[root@bogon opt]# ansible all -m shell -a "cat /etc/passwd|grep test" #查看
172.1.1.7 | FAILED | rc=1 >>
172.1.1.12 | FAILED | rc=1 >>
-
- shell模块:可以运行被控制端所有的shell命令,并能通过管道执行命令
实例:执行被控端机器下的shell脚本
[root@bogon opt]# ansible all -m shell -a "/tmp/a.sh" #确定好a.sh可执行,否则就
ansible all -m shell -a "chmod +x /tmp/a.sh" 加权限172.1.1.7 | SUCCESS | rc=0 >>
hello world
172.1.1.12 | SUCCESS | rc=0 >>
hello world
-
- script模块:用于执行ansible主控机器上的脚本到被控端机器
实例:执行ansbile的/root/a.sh到被控端机器上:
[root@bogon opt]# vim /root/a.sh
[root@bogon opt]# chmod +x /root/a.sh
[root@bogon opt]# ansible all -m script -a "/root/a.sh"
172.1.1.7 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.1.1.7 closed.\r\n",
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
172.1.1.12 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.1.1.12 closed.\r\n",
"stdout": "hello world\r\n",
"stdout_lines": [
"hello world"
]
}
- get_url模块:实现在远程主机上下载url到本地
实例:下载http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm到被控端服务器的tmp下:
[root@bogon opt]# ansible all -m get_url -a "url=http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm dest=/tmp/" #执行下载文件
172.1.1.12 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "7a524ad0ed6a57367980ea57a82298c04384c2d5",
"dest": "/tmp/epel-release-7-9.noarch.rpm",
"gid": 0,
"group": "root",
"md5sum": "a189bccb58e896c1501572e18e98d2eb",
"mode": "0644",
"msg": "OK (14704 bytes)",
"owner": "root",
"size": 14704,
"src": "/tmp/tmpn3Vj46",
"state": "file",
"uid": 0,
"url": "http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm"
}
172.1.1.7 | SUCCESS => {
"changed": true,
"checksum_dest": null,
"checksum_src": "7a524ad0ed6a57367980ea57a82298c04384c2d5",
"dest": "/tmp/epel-release-7-9.noarch.rpm",
"gid": 0,
"group": "root",
"md5sum": "a189bccb58e896c1501572e18e98d2eb",
"mode": "0644",
"msg": "OK (14704 bytes)",
"owner": "root",
"size": 14704,
"src": "/tmp/tmpSi85Qv",
"state": "file",
"uid": 0,
"url": "http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm"
}
[root@bogon opt]# ansible all -m command -a "ls -l /tmp" #查看下载文件
- yum模块:顾名思义就是用来管理yum仓库的:
- config_file:yum的配置文件
- disable_gpg_check:关闭gpg_check
- disablerepo:不启用某个yum源
- enablerepo:启用某个yum源
- name:要执行软件包的名字,可以传递一个本地url或者rpm包的路径
- state:执行状态如下
- absent|present|latest
实例:使用nginx的yum源安装nginx软件包,并进行检查
- 首先将nginx.repo文件写到被控制服务器的/etc/yum.repos.d/下:
-
[root@bogon tmp]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
3.然后再ansible服务器端执行:
[root@bogon opt]# ansible 172.1.1.12 -m yum -a "name=nginx enablerepo=nginx state=present" #安装
172.1.1.12 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * epel: mirrors.tuna.tsinghua.edu.cn\n * extras: mirrors.tuna.tsinghua.edu.cn\n * updates: mirrors.tuna.tsinghua.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package nginx.x86_64 1:1.10.3-1.el7.ngx will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n nginx x86_64 1:1.10.3-1.el7.ngx nginx 673 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 673 k\nInstalled size: 2.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1 \n----------------------------------------------------------------------\n\nThanks for using nginx!\n\nPlease find the official documentation for nginx here:\n* http://nginx.org/en/docs/\n\nCommercial subscriptions for nginx are available on:\n* http://nginx.com/products/\n\n----------------------------------------------------------------------\n Verifying : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1 \n\nInstalled:\n nginx.x86_64 1:1.10.3-1.el7.ngx \n\nComplete!\n"
]
}
[root@bogon opt]# ansible 172.1.1.12 -m shell -a "yum list installed|grep nginx" #查看安装
172.1.1.12 | SUCCESS | rc=0 >>
nginx.x86_64 1:1.10.3-1.el7.ngx @nginx
[root@bogon opt]# ansible 172.1.1.12 -m shell -a "yum remove nginx -y" #卸载nginx
172.1.1.12 | SUCCESS | rc=0 >>
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.10.3-1.el7.ngx will be erased
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
nginx x86_64 1:1.10.3-1.el7.ngx @nginx 2.3 M
Transaction Summary
================================================================================
Remove 1 Package
Installed size: 2.3 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1
Verifying : 1:nginx-1.10.3-1.el7.ngx.x86_64 1/1
Removed:
nginx.x86_64 1:1.10.3-1.el7.ngx
Complete!
- cron模块:定时任务模块
实战:定义webservers服务器组在每晚凌晨一点更新时间:
[root@bogon opt]# ansible all -m cron -a '"name=synctime" minute="*/5" job="/sbin/ntpdate time.windows.com >> /dev/null"'
172.1.1.7 | FAILED! => {
"changed": false,
"failed": true,
"msg": "unsupported parameter for module: \"name"
}
172.1.1.12 | FAILED! => {
"changed": false,
"failed": true,
"msg": "unsupported parameter for module: \"name"
- }
- [root@bogon ~]# ansible all -m cron -a "state=absent name=synctime" #删除计划任务
详细语法参考:ansible-doc cron
- service模块:服务启动关闭模块
实战:开启172.1.1.12的nginx服务
[root@bogon ~]# ansible 172.1.1.12 -m service -a "name=nginx state=started enable"