ansible
来源:互联网 发布:搜索引擎优化技巧 编辑:程序博客网 时间:2024/04/29 15:49
PlayBook-(1)
YAML语言介绍
参考链接:http://docs.ansible.com/ansible/YAMLSyntax.html
ansible里面的配置文件是通过YMAL文件来实现的。下面是YMAL语言的特点:
- 可读性高
- 和脚本语言交互好
- 使用的是实现语言的数据类型
- 有一个一致的信息模型
- 基于流来处理
- 表达能力强,可拓展性比较好
YAML语言语法特点:
- 下面是一个介绍YAML的 小例子:
name: Tom
age: 35
spouce:
name: Tik
age: 34
children:
- name: JK
age: 19
- name: HK
age: 12
说明:表示Tom今年35岁,有一个幸福的四口之家,妻子叫做Tik,今年34岁;俩孩子JK和HK活泼可爱。
接下来就把这上面的代码保存成一个test.yaml的文件放到和test.py同级的目录下,并且用python2.7执行即可。
python代码:
#!/usr/bin/python
import yaml
file = open('test.yaml')
read_file = yaml.load(file)
print read_file
执行结果:会传递出一个字典的格式出来
[root@bogon ~]# python test.py
{'age': 35, 'spouce': {'age': 34, 'name': 'Tik'}, 'name': 'Tom', 'children': [{'age': 19, 'name': 'JK'}, {'age': 12, 'name': 'HK'}]}
- YAML文件最常见的层次和结构,对应的就是python中的字典和列表两种类型:
还是用上面的python读取文件的例子来试试,生成列表
[root@bogon ~]# cat test2.yaml #yaml文件
- apple
- banana
- orange
- pear
[root@bogon ~]# cat test2.py #python文件
#!/usr/bin/python
import yaml
file = open('test2.yaml')
read_file = yaml.load(file)
print read_file
[root@bogon ~]# python test2.py #执行结果
['apple', 'banana', 'orange', 'pear']
再来一发玩玩吧:
node_a:
counttime: 300
external:
iface: eth0
port: 5566
internal:
iface: eth1
port: 5577
broadcast:
client: 1000
server: 99
node_b:
0:
ip: 10.1.1.1
name: b1
1:
ip: 10.1.1.2
name: b2
不行,老哥最后来一发
name: Example Developer
job: DeveLoper
skill: Elipt
employed: True
foods:
- apple
- orange
- mango
language:
ruby: Elit
python: Elit
dotnet: Lame
PlayBook
需要一个yaml格式的文件编排一个任务去执行。
- 组成部分(核心元素):
- hosts
- users
- 任务
- 变量
- 模板:包含模板语法的文本文件
- 处理器:有特定条件触发的任务
- 角色
- 基本组件:
- hosts:运行指定任务的目标主机
- remote_user:远程主机上执行任务的用户
- sudo_user:可选方式
- tasks:任务列表
- 模块,模块参数
- 格式:
- action:module arguments
- module:arguments
- 简单的小案例:
- 创建用户的小playbook:
- hosts: all
remote_user: root
tasks:
- name: create a user user3 #任务名称
user: name=user3 system=true uid=307 #执行任务
- name: create a user user4 #任务名称
user: name=user4 system=true uid=308
- 检查playbook
[root@bogon ~]# ansible-playbook --check first.yaml
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [create a user user3] *****************************************************
changed: [172.1.1.7]
changed: [172.1.1.12]
TASK [create a user user4] *****************************************************
changed: [172.1.1.7]
changed: [172.1.1.12]
PLAY RECAP *********************************************************************
172.1.1.12 : ok=3 changed=2 unreachable=0 failed=0
172.1.1.7 : ok=3 changed=2 unreachable=0 failed=0
[root@bogon ~]# ansible-playbook --list-hosts first.yaml
playbook: first.yaml
play #1 (all): allTAGS: []
pattern: [u'all']
hosts (2):
172.1.1.12
172.1.1.7
- 运行playbook
[root@bogon ~]# ansible-playbook first.yaml #执行
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [172.1.1.12]
ok: [172.1.1.7]
TASK [create a user user3] *****************************************************
changed: [172.1.1.7]
changed: [172.1.1.12]
TASK [create a user user4] *****************************************************
changed: [172.1.1.7]
changed: [172.1.1.12]
PLAY RECAP *********************************************************************
172.1.1.12 : ok=3 changed=2 unreachable=0 failed=0
172.1.1.7 : ok=3 changed=2 unreachable=0 failed=0
安装httpd使其监听在8080端口,
yaml文件如下
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: copy configure file
copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start service
service: name=httpd state=started
- name: execute ss command
shell: ss -tnl | grep 80
准备好httpd的配置文件:
测试执行:ansible-playbook --check httpd.yaml
正式执行:ansible-playbook httpd.yaml
- handlers:在特定条件下触发条件,重启服务
- 接收到其他任务的通知时才被触发
- task:任务的状态在运行后为chenged时,可通过notify通知给相应的handles
- 通过tags打标签,可以结合ansible-playbook -t 标签名称 yaml文件执行
改变配置文件之后重启httpd服务:
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: copy configure file
copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start service
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
- tag:打标签机制---通过执行标签对应的任务片段执行对应的task
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
tags: insthttpd
- name: copy configure file
copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: instconf
notify: restart httpd
- name: start service
service: name=httpd state=started
tags: restarthttpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
测试:ansible-playbook --check -t restarthttpd httpd3.yaml
执行:ansible-playbook -t restarthtpd httpd3.yaml
- variables:变量,只能以字母开头
- facts:用setup模块来获取facts的;可以直接调用
- ansible-playbook命令的命令行中的自定义变量
- -e VARS,--extra-vars=VARS
- 通过roles也能传递变量
- Host Inventory:实现想不通的主机传递不同的变量
- 向不同的主机传递不同的变量:IP/HOSTNAME varrable=value1 value2
- 向组中的主机传递相同的变量:
- [groupnames:vars]
- inventory参数:用于定义远程连接目标主机需要的参数,而非传递给playbook的参数
- ansible_ssh_host
- ansible_ssh_porty
- ansible_ssh_pass
- ansible_ssh_user
- ansible_sudo_pass
- 模板:
实现自定义安装软件包{{ pkgnames }}:
- hosts: webservers
remote_user: root
tasks:
- name: install {{ pkgs }}
yum: name={{ pkgs }} state=present
测试:ansible-playbook --check -e pkgs=memcached install_var.yaml
执行:ansible-playbook -e pkgs=memcached install_var.yaml
实现自定义修改主机名的操作:
1. 首先修改/etc/ansbile/hosts文件:
[root@bogon ~]# vim /etc/ansible/hosts
[webservers]
172.1.1.12 hname=www1
172.1.1.7 hname=www2
2.其次编写hostname.yaml文件
[root@bogon ~]# vim hostname.yaml
- - hosts: webservers remote_user: root tasks: - name: set hostname hostname: name={{ hname }}
3.测试: ansible-playbook --check hostname.yaml
4.执行: ansible-playbook hostname.yaml
playbook-(2)
playbook的其他元素
变量
- 变量:一定要记得{{ varname }}
- ansible facts
- ansible-playbook -e "var=value"
- host variable: host inventory
- group variable:
[groupname:vars]
var=value
- roles
- 变量的调用:{{ variable }}
模板(templates)
- 文本文件,嵌套有脚本(使用模板编程语言编写)
- jinjs2:基于Python
- 字面量:
- 字符串:使用单引号或者双引号
- 数字:整数和浮点数
- 列表
- 元组
- 字典
- 布尔值
- 算术运算:+ - * / // % **n
- 比较操作:== != > < >= <=
- 逻辑运算:and or not
- 字面量:
- template模块:基于模板的方式生成一个文件复制到远程主机
- *src:jinjia2文件路径(server端)
- *dest:客户端文件存放路径
获取虚拟CPU数量:
[root@bogon ~]# ansible all -m setup | grep ansible_processor_vcpus
"ansible_processor_vcpus": 1,
"ansible_processor_vcpus": 1,
nginx使用epel源安装实例:
1.在主控端下载好nginx的epel文件:
[root@bogon ~]# cat nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
2.然后epel文件推送到被控端
[root@bogon ~]# ansible all -m copy -a "src=~/nginx.repo dest=/etc/yum.repos.d/"
3.主控端执行安装命令
[root@bogon ~]# ansible all -m yum -a "name=nginx state=present"
4.配置文件模板推送
* 远程复制一个nginx.conf配置文件来当做模板文件,并重命名
[root@bogon ~]# scp 172.1.1.12:/etc/nginx/nginx.conf ./
nginx.conf 100% 643 0.6KB/s 00:00
[root@bogon ~]# mv nginx.conf nginx.conf.j2
*编辑模板文件:
- [root@bogon ~]# egrep -v "^#|^$" nginx.conf.j2 user nginx;worker_processes {{ ansible_processor_vcpus }};events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on;server { listen {{ http_port }}; server_name localhost; charset koi8-r; location / { root /usr/share/nginx/html; index index.html index.htm; }}}
*编辑hosts文件:
[root@bogon ~]# cat /etc/ansible/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
# - A hostname/ip can be a member of multiple groups
[webservers]
172.1.1.12 hname=www1
172.1.1.7 hname=www2
[webservers:vars]
http_port=8080
或者写成这样以便区分:
[root@bogon ~]# cat /etc/ansible/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
# - A hostname/ip can be a member of multiple groups
[webservers]
172.1.1.12 http_port=80
172.1.1.7 http_port=8080
*编辑yaml文件:
[root@bogon ~]# vim nginx.yaml
- hosts: webservers
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: install conf file
template: src=~/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
tags: instconf
- name: start nginx service
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
*检测执行:
[root@bogon ~]# ansible-playbook --check nginx.yaml
PLAY [webservers] **************************************************************
TASK [setup] *******************************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [install nginx] ***********************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [install conf file] *******************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [start nginx service] *****************************************************
changed: [172.1.1.7]
changed: [172.1.1.12]
PLAY RECAP *********************************************************************
172.1.1.12 : ok=4 changed=1 unreachable=0 failed=0
172.1.1.7 : ok=4 changed=1 unreachable=0 failed=0
没有报错的话就直接执行:ansible-playbook nginx.yaml
- 条件判断:
- when语句:在task中使用,jinja2语法格式
- tasks格式如下:
tasks:
- name: install conf to centos7
templete: src=~/nginx.c7.j2 dest=/etc/nginx/
when: ansible_distribution_major_version == "7"
- name: install conf to centos7
templete: src=~/nginx.c6.j2 dest=/etc/nginx/
when: ansible_distribution_major_version == "6"
根据操作系统的不同来配置和推送不同的配置文件:
*根据不同的操作系统拉取不同的配置文件:
[root@bogon ~]# scp 172.1.1.7:/etc/nginx/nginx.conf ./nginx.conf.c6.j2
nginx.conf 100% 700 0.7KB/s 00:00
[root@bogon ~]# scp 172.1.1.12:/etc/nginx/nginx.conf ./nginx.conf.c7.j2
nginx.conf 100% 702 0.7KB/s 00:00
*调整nginx.conf.c6.j2的配置文件;仍然要修改其worker_processor 对应的变量为:
worker_processes {{ ansible_processor_vcpus }};
listen {{ http_port }};
*调整nginx.yaml配置:
[root@bogon ~]# vim nginx.yaml
- hosts: webservers
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: install conf file
template: src=~/nginx.conf.c7.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "7"
notify: restart nginx
tags: instconf
- name: install conf file
template: src=~/nginx.conf.c6.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "6"
notify: restart nginx
tags: instconf
- name: start nginx service
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
*执行检查程序:ansible-playbook --check nginx.yaml
*无报错开始执行程序:ansible-playbook nginx.yaml
- 循环:迭代操作,需要执行重复执行的任务
- 对叠带项的引用,固定变量名为"item"
- 而后,要在task中使用with_items给定要迭代的元素列表
- with_items的形式有两种:
- 列表方法
- 字典方法
事例:
- name: install some package
yum: name={{ items }} state=present
with_items:
- nginx
- memcached
- php-fpm
迭代安装程序事例:
*编写yaml文件:
- host: allremote_user: root
tasks:
- name: install some packages
yum: name={{ item }} state=present
with_items:
- nginx
- memcached
- php-fpm
开始测试:ansible-playbook --check loop_test.yaml
执行任务:ansible-playbook loop_test.yaml
迭代创建用户和组,并进行关联(with_items的形式有两种的具体应用)
*编写yaml文件:
[root@bogon ~]#vim with_items.yaml
- host: all
remote_user: root
tasks:
- name: add some groups
group: name={{ item }} state=present
with_items:
- group11
- group12
- group13
- name: add some users
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'user11',group: 'group11' }
- { name: 'user11',group: 'group11' }
- { name: 'user11',group: 'group11' }
*测试执行:ansible-playbook --check with_items.yaml
*无报错最终执行:ansible-playbook with_items.yaml
playbook-(3)
角色
- 在网络中的主机如果要协调的过程,实现不同的分组之间的公共任务的执行,以减少重复执行。比如时间同步;
- 每一个角色就是一个目录;
(1): 角色的集合:
roles/
mysql/
httpd/
nginx/
memcached/
(2): 每个角色,以特定的层级目录结构进行组织
mysql/
files/ #存放由copy或者script模块等调用的文件
templates/ #template模块查找所需要的模板文件位置
tasks/ #至少包含一个main.yaml文件,其他文件的执行需要通过include包含
handlers/ #至少包含一个main.yaml的文件,
其他文件的执行需要通过include包含vars/ #
至少包含一个main.yaml的文件,
其他文件的执行需要通过include包含meta/ #至少包含一个main.yaml的文件,定义当前角色的特殊设定及其依赖关系
default/ #设定默认变量时使用此目录中的main.yaml文件
(3): 在playbook中调用角色:通过roles指定的任务,然后从上面的两个对应的角色集合中找到自己的角色和对应的任务进行执行
- hosts: webservers
remoute_user: root
roles:
- mysql
- ngins
- memcached
- redis
实例:创建一个nginx实例
(1)task文件
[root@bogon ~]# mkdir /etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars,default,meta} -pv
[root@bogon ~]# cd /etc/ansible/roles/nginx/
[root@bogon nginx]# vim tasks/main.yaml
- name: install nginx package
yum: name=nginx state=present
- name: install conf file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: start nginx
service: name=nginx state=started enabled=true
(2)templates文件
[root@bogon ~]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
user nginx;
worker_processes {{ ansible_processor_vcpus }};
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen {{ http_port }};
server_name localhost;
charset koi8-r;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
(3)写一个role文件
[root@bogon ~]# mkdir /ansible
[root@bogon ansible]# vim nginx.yaml
- hosts: webservers
remote_user: root
roles:
- nginx
(4)删除被控主机的nginx服务和配置
[root@www1 ~]# rpm -e nginx
warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave
warning: file /etc/nginx/conf.d/default.conf: remove failed: No such file or directory
[root@www1 ~]# rm -fr /etc/nginx/
[root@www1 ~]# rpm -e nginx
error: package nginx is not installed
(5)测试和执行
[root@bogon ~]# cd /ansible/
[root@bogon ansible]# ansible-playbook --check nginx.yaml #测试
[root@bogon ansible]# ansible-playbook nginx.yaml #执行
(6)触发器的创建
[root@bogon ansible]# vim /etc/ansible/roles/nginx/handlers/main.yaml
- name: restart nginx
service: name=nginx state=restarted
(7)tasks创建通知
[root@bogon ansible]# vim /etc/ansible/roles/nginx/tasks/main.yaml
- name: install nginx package
yum: name=nginx state=present
- name: install conf file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: start nginx
service: name=nginx state=started enabled=true
(8)模拟配置文件发生改变(修改j2文件)
[root@bogon ansible]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
user nginx;
worker_processes {{ ansible_processor_vcpus-1 }};
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen {{ http_port }};
server_name localhost;
charset koi8-r;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
(9)测试和执行
[root@bogon ~]# cd /ansible/
[root@bogon ansible]# ansible-playbook --check nginx.yaml #测试
[root@bogon ansible]# ansible-playbook nginx.yaml #执行
添加通知
(1)修改tasks配置
[root@bogon ansible]# vim /etc/ansible/roles/nginx/tasks/main.yaml
- name: install nginx package
yum: name=nginx state=present
- name: install conf file
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
tags: instconf
- name: start nginx
service: name=nginx state=started enabled=true
(2)测试和执行
[root@bogon ~]# cd /ansible/
[root@bogon ansible]# ansible-playbook -t instconf --check nginx.yaml #测试
[root@bogon ansible]# ansible-playbook -t instconf nginx.yaml #执行
var变量的使用
[root@bogon ansible]# vim /ansible/useradd.yaml
- hosts: webservers
remote_user: root
vars:
- groupname: testgroup1
- username: testuser1
tasks:
- name: create group
group: name={{ groupname }} state=present
- name: create user
user: name={{ username }} state=present
测试和执行
[root@bogon ~]# cd /ansible/
[root@bogon ansible]# ansible-playbook -t instconf --check useradd.yaml #测试
[root@bogon ansible]# ansible-playbook -t instconf useradd.yaml #执行
在playbook中定义变量的方式:
还是结合上述的ngin实例:
(1)定义vars变量文件
[root@bogon ansible]# vim /etc/ansible/roles/nginx/vars/main.yaml
- username: daemon
(2)修改j2文件:
[root@bogon ansible]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2
user {{ username }};
worker_processes {{ ansible_processor_vcpus-1 }};
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen {{ http_port }};
server_name localhost;
charset koi8-r;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
(3)测试和执行
[root@bogon ~]# cd /ansible/
[root@bogon ansible]# ansible-playbook -t instconf --check nginx.yaml #测试
[root@bogon ansible]# ansible-playbook -t instconf nginx.yaml #执行在
在playbook中调用角色的方法:
(1)在playbook中调用角色:通过roles指定的任务,然后从上面的两个对应的角色集合中找到自己的角色和对应的任务进行执行
- hosts: webservers
remoute_user: root
roles:
- mysql
- ngins
- memcached
- redis
(2)通过变量指明方法:其中键role用于指定角色名称;后续的k/v用于传递变量给角色
还可以基于条件测试实现角色调用,
- hosts: webservers
remote_user: root
roles:
-
{ role: nginx,username :nginx }
第二种方法的实例(不指定条件):
[root@bogon ~]# vim /ansible/nginx.yaml
- hosts: webservers
remote_user: root
roles:
- { role: nginx,username :nginx }
执行程序:
[root@bogon ~]# ansible-playbook --check nginx.yaml #执行
PLAY [webservers] **************************************************************
TASK [setup] *******************************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [install nginx] ***********************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [install conf file] *******************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
TASK [start nginx service] *****************************************************
ok: [172.1.1.7]
ok: [172.1.1.12]
PLAY RECAP *********************************************************************
172.1.1.12 : ok=4 changed=0 unreachable=0 failed=0
172.1.1.7 : ok=4 changed=0 unreachable=0 failed=0
[root@bogon ~]# ansible all -m shell -a "ps -ef | grep nginx" #返回结果
172.1.1.7 | SUCCESS | rc=0 >>
root 10412 1 0 10:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 10414 10412 0 10:21 ? 00:00:00 nginx: worker process
root 12949 12948 0 11:00 pts/1 00:00:00 /bin/sh -c ps -ef | grep nginx
root 12951 12949 0 11:00 pts/1 00:00:00 grep nginx
172.1.1.12 | SUCCESS | rc=0 >>
root 4080 1 0 18:06 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 4081 4080 0 18:06 ? 00:00:00 nginx: worker process
root 5013 5008 0 18:54 pts/2 00:00:00 /bin/sh -c ps -ef | grep nginx
root 5015 5013 0 18:54 pts/2 00:00:00 grep nginx
第二种方法的实例(指定条件)
- hosts: webservers
remote_user: root
roles:
- { role: nginx,username: nginx,when:"ansible_distribution_major_version == '7'" }按条件安装
按条件安装memcached软件:
创建角色:
[root@bogon ~]# mkdir -pv /etc/ansible/roles/memcached/tasks
[root@bogon tasks]# cd /etc/ansible/roles/memcached
[root@bogon memcached]# vim tasks/main.yaml
- name: install package
yum: name=memcached state=present
- name: start memcached
service: name=memcached state=started enabled=true
创建执行入口文件
[root@bogon memcached]# cd /ansible/
[root@bogon memcached]#
vim nginx_memcached.yaml- hosts: all
remote_user: root
roles:
- { role: nginx, when: ansible_distribution_major_version == '7' }
- { role: memcached, when: ansible_hostname == 'memcached' }
模板文件的创建:
[root@bogon ansible]# cd /etc/ansible/roles/memcached/
[root@bogon memcached]# mkdir templates
[root@bogon memcached]# scp 172.1.1.7:/etc/sysconfig/memcached ./templates/
[root@bogon memcached]# vim templates/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb//4 }}"
OPTIONS=""
[root@bogon memcached]# cd templates/
[root@bogon templates]# mv memcached memcached.j2
再次修改角色文件:
[root@bogon templates]# vim ../tasks/main.yaml
- name: install package
yum: name=memcached state=present
- name: install conf
template: src=memcached.j2 dest=/etc/sysconfig/memcached
notify: restart memcached
tags: memconf
- name: start memcached
service: name=memcached state=started enabled=true
创建handlers文件:
[root@bogon memcached]# mkdir handlers
[root@bogon memcached]# vim handlers/main.yaml
- name: restart memched
service: name=memcached state=restarted
执行测试:
[root@bogon ansible]# ansible-playbook --check nginx_memcached.yaml
[root@bogon ansible]# ansible-playbook -t memconf --check nginx_memcached.yaml
验证:
[root@bogon ansible]# ansible all -m shell -a "cat /etc/sysconfig/memcached"
172.1.1.7 | SUCCESS | rc=0 >>
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="245"
OPTIONS=""
站点:www.ansible.com.cn
阅读全文
1 0
- ansible
- ansible
- Ansible
- Ansible
- ansible
- Ansible
- ansible
- Ansible
- ansible
- ansible
- Ansible
- Ansible
- Ansible
- ansible
- ansible
- ansible
- ansible
- ansible
- PHP array_column() 函数
- MKNetWorkKit使用方法
- Valid Square
- C# 数组去重的三种方法
- Maven快照机制(SNAPSHOT)
- ansible
- SpringBoot打包jar、war部署项目
- Android透明度数值记录
- Codeforces Round #418 (Div. 2) C 预处理/思维题
- 善变的女人
- Storage事件无法触发解决
- RxJava基础,从认识到会用只需这篇
- java发送邮件
- /usr/lib64/python2.7/site-packages/sklearn/utils/validation.py:395 DeprecationWarning