ansible原理及操作

ansible原理

简介

ansible是一款基于python开发的自动化运维工具,适用于中小型应用环境。可实现如下功能:

(1)自动化部署APP;
(2)自动化管理配置项;
(3)自动化的持续交互;
(4)自动化的(AWS)云服务管理;

从本质上来讲就是在一台或多台远程服务器上,执行一系列的命令。

ansible命令执行过程

(1) 加载自己的配置文件,默认为/etc/ansible/ansible.cfg
(2) 加载自己对应的模块文件,如command
(3) 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
(4)给文件+x执行
(5)执行并返回结果
(6)删除临时py文件,sleep 0退出
执行状态:
(1)绿色:执行成功并且不需要做改变的操作
(2)黄色:执行成功并且对目标主机做变更
(3)红色:执行失败

1.用ansible在多台机器上添加用户

[root@centos7 ansible]ansible nginxsrvs -m user -a 'name=user1 comment="test user" uid=11211 home=/home/user1 group=root'
192.168.48.131 | CHANGED => {
    "changed": true, 
    "comment": "test user", 
    "create_home": true, 
    "group": 0, 
    "home": "/home/user1", 
    "name": "user1", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 11211
}
192.168.48.132 | CHANGED => {
    "changed": true, 
    "comment": "test user", 
    "create_home": true, 
    "group": 0, 
    "home": "/home/user1", 
    "name": "user1", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 11211
}
[root@centos7 ansible]ansible nginxsrvs -m shell -a 'id user1'
192.168.48.131 | CHANGED | rc=0 >>
uid=11211(user1) gid=0(root) groups=0(root)

192.168.48.132 | CHANGED | rc=0 >>
uid=11211(user1) gid=0(root) groups=0(root)

2.用ansible安装nginx服务

1.安装ansible并配置主机清单

[root@centos7 ~]yum install -y ansible
[root@centos7 ~]vim /etc/ansible/hosts
······
[nginxsrvs]
192.168.48.131 
192.168.48.132

2.简单搭建roles

搭建roles目录结构
[root@centos7 ~]mkdir /etc/ansible/roles/nginx
[root@centos7 ~]cd !$
cd /etc/ansible/roles/nginx
[root@centos7 nginx]mkdir {files,templates,tasks,handlers,vars,meta}
[root@centos7 nginx]ls
filrs  handlers  meta  tasks  templates  vars
[root@centos7 nginx]cd tasks

group配置
[root@centos7 tasks]vim group.yml
- name: create group
  group: name=nginx gid=80

user配置
[root@centos7 tasks]vim user.yml
- name: cerate user
  user: name=nginx uid=80 group=nginx system=yes shell=/sbin/nologin

yum安装配置
[root@centos7 tasks]vim yum.yml
- name: install nginx
  yum: name=nginx state=present

服务启动配置
[root@centos7 tasks]vim start.yml
- name: start service
  service: name=nginx state=started 

服务重启配置
[root@centos7 tasks]vim ../handlers/main.yml
- name: restart service
  service: name=nginx state=restarted

复制nginx原版配置文件到templates目录并重命名作为模板
[root@centos7 tasks]cp /etc/nginx/nginx.conf ../templates/nginx.conf.j2

修改启动进程数是cpu数量的两倍
vim ../templates/nginx.conf.j2
work_processes {{ ansible_processor_vcpus * 2 }}

模板调用配置
[root@centos7 tasks]vim templ.yml
- name: copy conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart service
nginx 总配置用于调用之前所有配置
[root@centos7 tasks]vim main.yml
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: start.yml

编写nginx调用剧本
[root@centos7 tasks]cd /etc/ansible
[root@centos7 ansible]vim nginx_role.yml
- hosts: nginxsrvs
  remote_user: root
  roles:
    - role: nginx

[root@centos7 ansible]tree roles/nginx
roles/nginx
├── filrs
├── handlers
│   └── main.yml
├── meta
├── tasks
│   ├── group.yml
│   ├── main.yml
│   ├── start.yml
│   ├── templ.yml
│   ├── user.yml
│   └── yum.yml
├── templates
│   └── nginx.conf.j2
└── vars

3.测试安装

[root@centos7 ansible]ansible-playbook nginxsrvs nginx_role.yml
[root@centos7 ansible]ansible-playbook nginx_role.yml 

PLAY [nginxsrvs] ********************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [192.168.48.132]
ok: [192.168.48.131]

TASK [nginx : create group] *********************************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

TASK [nginx : cerate user] **********************************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

TASK [nginx : install nginx] ********************************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

TASK [nginx : copy conf] ************************************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

TASK [nginx : start service] ********************************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

RUNNING HANDLER [nginx : restart service] *******************************************************************
changed: [192.168.48.131]
changed: [192.168.48.132]

PLAY RECAP **************************************************************************************************
192.168.48.131             : ok=7    changed=6    unreachable=0    failed=0   
192.168.48.132             : ok=7    changed=6    unreachable=0    failed=0

[root@centos7 ansible]ansible nginxsrvs -m shell -a 'ps aux|grep nginx'
192.168.48.132 | CHANGED | rc=0 >>
root       5146  0.0  0.1 122908  2104 ?        Ss   20:42   0:00 nginx: master process /usr/sbin/nginx
nginx      5147  0.0  0.2 125376  3564 ?        S    20:42   0:00 nginx: worker process
nginx      5148  0.0  0.2 125376  3564 ?        S    20:42   0:00 nginx: worker process
root       5257  0.0  0.0 113128  1204 pts/1    S+   20:46   0:00 /bin/sh -c ps aux|grep nginx
root       5259  0.0  0.0 113128   188 pts/1    R+   20:46   0:00 /bin/sh -c ps aux|grep nginx

192.168.48.131 | CHANGED | rc=0 >>
root       8298  0.0  0.1 122928  2104 ?        Ss   20:42   0:00 nginx: master process /usr/sbin/nginx
nginx      8299  0.0  0.2 125472  3560 ?        S    20:42   0:00 nginx: worker process
nginx      8300  0.0  0.2 125472  3560 ?        S    20:42   0:00 nginx: worker process
root       8424  0.0  0.0 113128  1204 pts/1    S+   20:46   0:00 /bin/sh -c ps aux|grep nginx
root       8426  0.0  0.0 113128   188 pts/1    R+   20:46   0:00 /bin/sh -c ps aux|grep nginx

[root@centos7 ansible]ansible nginxsrvs -m shell -a 'lscpu | grep ^CPU\(s\)'
192.168.48.131 | CHANGED | rc=0 >>
CPU(s):                1

192.168.48.132 | CHANGED | rc=0 >>
CPU(s):                1
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容