ansible日常入门指南

ansible的一些基本用法

ansible

特性

模块化:调用特定的模块,完成特定任务

有paramiko,pyYAML,   jinja2(模板语言)三个关键模块

支持自定义模块

基于Python语言实现

部署简单,基于Python和SSH(默认已安装),agentless

安全,基于openssh

支持playbook编排任务

幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况

无需代理不依赖PKI(无需ssl)

可使用任何编程语言写模块

YAML格式,编排任务,支持丰富的数据结构

较强大的多层解决方案

Ansible主要组成部分

Ansible playbooks:任务剧本,编排定义ansible任务集的配置文件,由ansible顺序依次执行,通常是JSON格式的YML文件

Inventory:ansible管理主机的清单/etc/anaible/hosts

MODULES:ansible执行命令的功能模块,多数为内置的核心模块,也可自定义

Plugins 模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用

API:供第三方程序调用的应用程序编程接口

ANSIBLE:组合inventory、api。Modules

Plugins的绿框,可以理解为是ansible命令工具,其核心执行工具

Ansible命令执行来源

USER,普通用户,即SYSTEM ADMINISTRATOR

CMDB(资产管理系统),API调用

PUBLIC/PRIVATE CLOUD API 调用

USER–>ANSIBLE PLAYBOOK –>ANSIBILE

利用ansible细线管理的方式

Ad-hoc 即ansible命令,主要用于临时命令使用场景

Ansible-playbook 主要用于长期规划好的,大型项目的场景,需要有前提的规划

 

 

[root@centos7 ~]# Hostnamectl   set-hostname  ansible   该主机名字,

[root@centos7 ~]#exec bash

[root@centos7 ~]# vim /etc/ansible/hosts   将被管理主机加入主机清单

[root@centos7 ~]# vim /etc/ansible/ansible.cfg

host_key_checking = False  修改注释  不验证

 

[root@centos7 ~]# ansible 192.168.27.130 -m ping -k 执行 测试 是否处于连接状态

[root@centos7 ~]# vim /etc/ansible/hosts

[web]

192.168.27.130

[db]

192.168.27.120

[root@centos7 ~]# ansible web -m ping -k

基于k验证

[root@centos7 ~]# ssh-keygen

[root@centos7 .ssh]# ssh-cop-id 192.168.27.120

[root@centos7 .ssh]# ssh-copy-id 192.168.27.130

[root@centos7 .ssh]# vim /etc/ansible/ansible.cfg

启动日志功能

log_path = /var/log/ansible.log

Ansible不受ssh服务的影响,但是受network的影响

[root@centos7 ~]# ansible web -m command -a “ls /root”   一root身份连接

Command命令模块

[root@centos7 ~]# ansible db -m command -a “ls /app”

[root@centos7 ~]# ansible db -m command -a “removes=/etc/fstab ls /app” 不存在就不执行

[root@centos7 ~]# ansible db -m command -a “creates=/etc/fstab ls /app”存在就不执行

[root@centos7 ~]# ansible web -m command -a ‘chdir=/app ./f1.sh’   执行脚本

 

Ansible db -m shell -a ‘echo magedu | passwd –stdin test1’ 修改用户test1的密码

Ansible db -m copy -a ‘src=/etc/selinux/config dest=/etc/selinux/ backup=yes’  远程拷贝文件

Ansible db -m copy -a ‘src=/etc/fstab dest=/app/fstab2 mode=600 owner=test1’ 改权限

Ansible db -m copy -a ‘src=/etc/sysconfig dest=/app/’ 复制目录

Ansible db -m copy -a ‘content=”de -h\nhostname\nls\n” dest=/app/f1.sh’  将内容复制到文件

 

[root@centos7 ~]# ansible db -m fetch -a ‘src=/etc/passwd dest=/app/’  将远程的文件抓到我的主机上

[root@centos7 ~]# ansible db -m file -a ‘path=/app/testfile state=touch mode=600 owner=test1’  创建空文件

 

[root@centos7 ~]# ansible db -m file -a ‘path=/app/dir2 state=directory’ 创建空文件夹

[root@centos7 ~]# ansible db -a ‘ls -l /app’   查看

Ansible db -m shell -a ‘ls -la /app’

[root@centos7 ~]# ansible 192.168.27.120 -m hostname -a ‘name=centos7-120.magedu.com’  该主机名

 

Ansible-vault encrypt file.yml  加密文件

Ansible-vault view file.yml 看文件

Ansible-vault edit file.yml 编辑文件

Ansible-vault rekey file.yml  更改口令

Ansible-vault decrypt file.yml   解密文件

 

 

 

[root@centos7 ~]# ansible-galaxy install geerlingguy.nginx

[root@centos7 ~]# cd /etc/ansible/roles/geerlingguy.nginx/

[root@centos7 geerlingguy.nginx]# cd tasks

[root@centos7 tasks]# cat setup-RedHat.yml     playbook

 

Playbook是由一个或多个play组成的列表

Play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,既可以让他们连同起来按事先编排的机制同唱一台大戏

Playbook采用yaml语言编写

YAML语法简介

在单一档案中,可用连续三个连字号(—)区分多个档案。另外,还有选择性的连续三个点号(…)用来表示档案结尾

次行开始正常写playbook的内容,一般建议写明该playbook的功能

使用#号注释代码

缩进必须是统一的,不能空格和tab混用

缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结合换行来实现的

YAML文件的内容和LINUX系统大小写判断方式保持一致,是区别大小写的,k/v的值均需大小写敏感

K/v的值可同行写也可换行写。同行使用:分隔

V可是个字符串,也可是另一个列表

一个完整的代码块功能需最少元素包括name:task

一个name只能包括一个task

YAML文件扩展通常为yml或yaml

Ansible all -m yum -a ‘name=vsftpd’ state=absent    卸载服务

 

 

[root@centos7 ~]# vim test1.yml   在远程服务器上安装一个软件包

[root@centos7 ~]# ansible-playbook -C test1.yml  测试运行

 

– hosts: db

remote_user: root

 

tasks:

– name: install package

yum: name=httpd state=present

– name: start service

service: name=httpd state=started enabled=yes

[root@centos7 ~]# ansible-playbook test1.yml   运行脚本

 

[root@centos7 ~]# cp test1.yml test2.yml

[root@centos7 ~]# vim test2.yml

– hosts: db

remote_user: root

 

tasks:

– name: install package

yum: name=httpd state=present

– name: start service

service: name=httpd state=started enabled=yes

– hosts: web

remote_user: root

 

tasks:

– name: copy file

copy: src=/etc/fstab dest=/app/ owner=wang mode=600

– name: create user

user: name=test3 shell=/sbin/nologin system=yes

[root@centos7 ~]# ansible-playbook -C test2.yml  测试运行

 

[root@centos7 ~]# vim httpd.yml   装包yum  配置文件覆盖    启动服务

#

– hosts: web

remote_user: root

 

tasks:

– name: install httpd

yum: name=httpd

– name: copy config file

copy: src=/app/httpd.conf dest=/etc/httpd/conf/ backup=yes

– name: start httpd

service: name=httpd state=started enabled=yes

Ansible-playbook httpd.yml  运行

 

 

#

– hosts: web

remote_user: root

 

tasks:

– name: install httpd

yum: name=httpd

– name: copy config file

copy: src=/app/httpd.conf dest=/etc/httpd/conf/ backup=yes

notify: restart httpd

– name: start httpd

service: name=httpd state=started enabled=yes

handlers:

– name: restart httpd

service: name=httpd state=restarted

第一次,执行照常执行;第二次执行时,notify 触发handlers执行

Handlers

是task列表,这些task与前述的task并没有本质上的不同,用于当关注的资源发生变化时,才会采取一定的操作

Notify这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,仅仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作

Ansible -playbook –tages copyconf -C httpd.yml  从我的剧本中挑出tages代表的我的任务执行,其他不执行

 

 

 

 

[root@centos7 ~]# ansible all –list-hosts     主机管理的所有列表

[root@centos7 ~]# ansible all -m setup     列出主机对应的所用信息

[root@centos7 ~]# ansible all -m setup |grep version   列出版本信息

Playbook中变量使用

变量名:仅能由字母,数字和下划线组成,且只能以字母开头

变量来源

  • ansible setup facts 远程主机的所有变量都可直接调用
  • 在/etc/ansible/host中定义

普通变量:主机组中主机单独定义,优先级高于公共变量

公共变量:针对主机组中所有主机定义统一变量

  • 通过命令行指定变量,优先级最高  ansible-playbook -e varname=value
  • 在playbook中定义
  • Vars:

 

 

[root@centos7 ~]# vim var1.yml

– hosts: web

remote_user: root

 

tasks:

– name: install package

yum: name={{ pkname }}

[root@centos7 ~]# ansible-playbook -e pkname=vsftpd var1.yml            可以传一个值  

[root@centos7 ~]# ansible web -m shell -a ‘rpm -qa|grep vsftpd’  查找看是否装了 vsftpd

 

[root@centos7 ~]# vim var1.yml

– hosts: web

remote_user: root

 

tasks:

– name: install package

yum: name={{ pkname }}

– name: copy file

copy: src=/app/{{ filename }} dest=/app/

[root@centos7 ~]# ansible-playbook -e “pkname=htop filename=httpd.conf” var1.yml   使用多个变量

[root@centos7 ~]# ansible web -m yum -a ‘name=vsftpd,htop state=absent’   删除两个服务

 

 

 

[root@centos7 ~]# vim var2.yml    在playbook中定义变量和使用变量

– hosts: web

remote_user: root

vars:

– username: user1

– groupname: group1

 

tasks:

– name: create group

group: name={{ groupname }}

– name: create user

user: name={{ username }} group={{ groupname }} home=/app/{{ username }}dir

[root@centos7 ~]# ansible-playbook var2.yml   运行

 

[root@centos7 ~]# vim var3.yml   创建文件

– hosts: web

remote_user: root

 

tasks:

– name: create file

file: name=/app/{{ ansible_hostname }}.txt state=touch

[root@centos7 ~]# ansible web -a ‘la /app/’  运行

[root@centos7 ~]# ansible web -a ‘ls /app/’  检查文件是否生成

 

[root@centos7 ~]# vim /etc/ansible/hosts  在配置文件中定义

[web]

192.168.27.130 http_port=85

[root@centos7 ~]# ansible web -m hostname -a ‘name=web{{http_port}}’

[root@centos7 ~]# hostname

web85

  普通变量

[root@centos7 ~]# vim /etc/ansible/hosts

[web]

192.168.27.130 http_port=85 hname=httpd

[root@centos7 ~]# vim var4.yml

– hosts: web

remote_user: root

 

tasks:

– name: set hostname

hostname: name={{hname}}-{{http_port}}

[root@centos7 ~]# ansible-playbook -C var4.yml

公共变量

[root@centos7 ~]# vim /etc/ansible/hosts

[web]

Hname=web

 

模板templates

文本文件,嵌套有脚本(使用模板编程语言编写)

Jinjia2语言,使用字面量,有下面形式

字符串:使用单引号或双引号

数字:整数,浮点数

 

复制功能

[root@centos7 templates]# vim tmphttpd.yml

– hosts: web

remote_user: root

 

tasks:

– name: template

template: src=httpd.conf.j2 dest=/app/httpd.conf

~

[root@centos7 templates]# ansible-playbook tmphttpd.yml

 

 

 

 

[root@centos7 templates]# cd /etc/ansible

[root@centos7 ansible]# cp /etc/nginx/nginx.conf templates/nginx.conf.j2

[root@centos7 ansible]# cp tmphttpd.yml tmpnginx.yml

[root@centos7 ansible]# vim tmpnginx.yml

– hosts: web

remote_user: root

 

tasks:

– name: install nginx

yum: name=nginx

– name: template

template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

tags: instconf

– name: start service

service: name=nginx state=started

~

[root@centos7 ansible]# ansible-playbook -C tmpnginx.yml

[root@centos7 templates]# ansible web -m shell -a ‘ss -ntl|grep :80’  查看80 端口 在客户端

 

实现centos6和centos7上的httpd安装和启动

[root@centos7 ansible]# vim tmphttpd.yml

 

– hosts: all

remote_user: root

 

tasks:

– name: install httpd

yum: name=httpd

 

– name: template 6

template: src=httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf

when: ansible_distribution_major_version==”6″

 

– name: template 7

template: src=httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf

when: ansible_distribution_major_version==”7″

– name: start service

service: name=httpd state=started

[root@centos7 ansible]# ansible-playbook -C tmphttpd.yml  试运行

 

[root@centos7 ansible]# vim  tmphttpd.yml  使用when

– hosts: all

remote_user: root

tasks:

– name: install httpd

yum: name=httpd

– name: template 6

template: src=httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf

when: ansible_distribution_major_version==”6″

– name: template 7

template: src=httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf

when: ansible_distribution_major_version==”7″

– name: start service

service: name=httpd state=started

迭代:with_items

迭代:当有需要重复性执行的任务时,可以用迭代机制

对迭代项的引用,固定变量名为“item”

要在task中使用with_item给定要迭代的元素列表

列表格式  字符窜,字典

[root@centos7 ansible]# vim item1.yml

– hosts: web

remote_user: root

 

tasks:

– name: create servel user

user: name={{item}} group=root groups=wang,bin

with_items:

– itemuser1

– itemuser2

[root@centos7 ansible]# ansible-playbook -C item1.yml   检测执行

[root@centos7 templates]# ansible web -m shell -a ‘getent passwd |tail -n2’  查看生成的用户

复制文件,迭代

[root@centos7 ansible]# vim item2.yml

– hosts: web

remote_user: root

tasks:

– name: copy conf files

copy: src={{item}} dest=/app/

with_items:

– /app/file1

– /app/file2

– /app/file3

– name: install packages

yum: name={{item}}

with_items:

– vsftpd

– memcached

– hping3

 

For循环

[root@centos7 ansible]# vim templates/for1.conf.j2

– hosts: web

remote_user: root

vars:

ports:

– 81

– 82

– 83

tasks:

– name: test for1

template: src=for1.conf.j2 dest=/app/for1.conf

[root@centos7 ansible]# vim templates/for1.conf.j2  写模板

{%for port in ports %}

server {

listen port;

}

{%for port in ports %}

server {

listen {{port}};

}

{%endfor%}

[root@centos7 ansible]# ansible-playbook -C for1.yml  运行

另一种方法

[root@centos7 ansible]# vim  for2.yml

 

– hosts: web

remote_user: root

vars:

ports:

– listen_port: 81

– listen_port: 82

– listen_port: 83

tasks:

– name: test for2

template: src=for2.conf.j2 dest=/app/for2.conf

[root@centos7 ansible]# vim  templates/for2.conf.j2

 

{%for port in ports %}

server {

listen {{port.listen_port}};

}

{%endfor%}

使用if 定义的时候使用该行,不定义的时候就嵌入代码,

[root@centos7 ansible]# vim for4.yml

– hosts: web

remote_user: root

vars:

vhosts:

– web1:

port: 81

#name: web1.magedu.com

root: /app/webroot1

– web2:

port: 82

name: web2.magedu.com

root: /app/webroot2

– web3:

port: 83

#name: web3.magedu.com

root: /app/webroot3

tasks:

– name: test for1

template: src=for4.conf.j2 dest=/app/for4.conf

定义模板

[root@centos7 ansible]# vim templates/for4.conf.j2

 

{%for vhost in vhosts %}

server {

listen {{vhost.port}};

{%if vhost.name is defined%}

servername {{vhost.name}};

{%endif%}

rootdir {{vhost.root}};

}

{%endfor%}

roles

Ansible自1.2版本引入的新特性,用于层次性、结构化地组织playbook。Roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include他们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守候进程等场景中

复杂场景:建议使用roles,

变更指定主机或主机组

如命令不规范维护和传承成本大

某些功能需多个playbook,通过include即可实现

 

实现文件复制的角色

[root@dnsclient roles]# mkdir filecopy/tasks -pv

[root@dnsclient roles]# mkdir filecopy/files

[root@dnsclient ansible]# vim roles/filecopy/tasks/main.yml

 

– name: file copy

copy: src=fstab dest=/app/

 

– name: file create

file: name=/app/testfile mode=600 state=touch

[root@dnsclient ansible]# vim filecopy-role.yml

 

– hosts: web

remote_user: root

 

roles:

– role: filecopy

[root@dnsclient ansible]# ansible-playbook filecopy-role.yml  -C   试运行

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91295

(2)
上一篇 2018-01-23 09:01
下一篇 2018-01-28 21:35

相关推荐

  • 马哥教育网络班19期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。     网桥用来连接两个不同的网络间的设备;集线器的作用是把接受的信号放大,从另一个端口再穿法出去,说白了就是延长网络的传输距离;   二层交换机工作在数据链路层,根据源和目的MAC地址进行数据转发;三层交换机就是具有部分路由器功能的交换机,三层交换…

    Linux干货 2016-07-07
  • user相关的一些命令及用法

    前言            我们知道,当我们进入一家公司做运维方面的工作的时候,我们需要对公司的服务器进行监控管理,换句话说,我们需要一个身份来操作服务器。那么我们又知道,linux系统下拥有最高权限的管理员是Root,它无所不能,无所畏惧,那么公司肯定就是不会把这个账户交给你使用。那么就引出一个概…

    2017-07-22
  • 网络服务基础理论

    最近学习了有关网络管理的有关知识,所以给也在学习这些知识的童鞋分享一下我的一些经验。 听了这几天课,总结了几个前提。 有关网络的通信均是基于数据传输的,且数据均是二进制格式的流式数据。 在网络中需要把大包分成小包, 每传一个包都需要有三个地址,由内向外为端口号、IP地址、MAC地址。端口号确定应用,IP确定网段,MAC确定广播域中的某个网卡。 OSI模型 &…

    Linux干货 2017-09-01
  • linux上的LVM简明教程

    LVM是一个多才多艺的硬盘系统工具。在Linux上非常的好用,传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。 首先是实际的物理磁盘及其划分的分区和其上的物理卷(PV…

    Linux干货 2017-05-02
  • Linux第一周学习心得

                           LINUX入门学习心得 Shell基本命令 ### shell可执行命令分类 ### 内部命令:由shell自带得,而且通过某命令形式提供 help 内部命令列表 enble cmd 启用内部命…

    Linux干货 2017-07-15
  • N25-第二周做业

    一.Linux上的文件管理类命令都有哪些,其常用的使用方法和演示        1.目录管理的命令    mkdir rmdir              1)…

    Linux干货 2016-12-11