ansible笔记

第一章:ansible入门

ansible软件特性:

是一个基于Python开发的自动化运维工具,

其功能实现基于SSH远程连接服务,

ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能,

不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端。

不需要服务端(no servers)。

需要依靠大量的模块实现批量管理。? 

配置文件/etc/ansible/ansible.cfg,不用配置。

ansible软件执行结果

输出内容显示绿色:表示执行成功,当没有任何改变

输出内容显示黄色:表示执行成功,但对被管理主机进行了改变

输出内容显示红色:表示执行失败!!!

1、安装ansible

在管理端安装ansible

yum install epel-release -y

yum install ansible -y

配置从管理节点到远程主机之间基于密钥(无密码的方式)的 SSH 连接

生成ssh密钥  ssh-keygen -t rsa(加密方式有2种 rsa和dsa,这里使用rsa)

#然后一直回车会生成密钥文件和私钥文件id_rsa,id_rsa.pub,存放在/root/.ssh 目录下需要ll -a 才能查看到

复制ssh密钥到远程主机:ssh-copy-id  远程用户@远程主机IP (.pud文件复制到远程直接的.ssh目录

这样就可以使用ssh不需要输入密码了

ssh远程方法:ssh 远程用户@远程主机

SSH 的时候不会提示是否保存密钥  ssh-keyscan remote_servers >~/ . ssh/known_hosts

被远程的主机不需要特殊设置,只要支持 ssh服务和python2.4以上版本就可以,也可以不使用无密码方式,使用ansible的时候会提示输入密码。

2、ansible管理哪些主机

Ansible 需要知道自己可以管理的主机有哪些,以及一些连接参数,它由主机目录配置文件
来管理。

主机目录默认的文件是 /etc/ansible/hosts

如:

[组名]下面是机器IP地址或域名(需要域名解析),如果IP地址都是连续的可以写成:192.168.1.[1:100] 表示从1到100的IP地址。

3、ansible用命令管理主机

ansible <host-pattern> [-m module_name] [-a args] [options]

<host-pattern> 自己定义的主机组

[-m module_name] 指定模块
[-a args] 指定要执行的动作

4、ansible用脚本管理主机

为了避免重复的输入命令,ansible提供了脚本功能,ansible脚本的名字叫playbook,使用的是YAML格式,文件以yml或yaml为后缀。

执行脚本的方法:ansible-playbook deploy.yml

4.1、playbook包含几个关键字:

1、hosts: 某主机的IP或者是主机组名,或all所有

2、remote_user: 以某个用户身份执行

3、vars: 变量

4、tasks: playbook的核心,定义顺序执行的动作action。每个action调用一个ansible模块

            5、handers: playbook的event处理操作,有且仅有在被action触发时才会执行,但多次触发只执行一次,并按照声明的顺序执行

写一个为主机组部署apache的deploy.yml文件部署步骤如下

1.安装Apache包;2.复制配置文件httpd,3.复制默认的网页文件;4.启动Apache服务

注意格式要求:

---

- hosts: web

            vars:

                       http port: 80

                       max clients: 200

            remote user: root

            tasks:

            - name: ensure apache is at the latest version

         yum : pkg=httpd state=latest

            - name : Write the configuration file

                       template : src=templates/httpd. conf . j2 dest=/etc/httpd/conf/httpd.conf

                       notify :

                       - restart apache

            - name : Write the default index . html file

                       template : src=templates/index . html.j2 dest=/var/www/html/index . html

            - name : ensure a pache is running

                       service : name=httpd state=started

            handlers :

                       - name : restart apache

                                   service : name=httpd state=restarted

json的格式

[

  {

    "hosts": "web",

    "vars": {

      "http port": 80,

      "max clients": 200

    },

    "remote user": "root",

    "tasks": [

      {

        "name": "ensure apache is at the latest version",

        "yum": "pkg=httpd state=latest"

      },

      {

        "name": "Write the configuration file",

        "template": "src=templates/httpd. conf . j2 dest=/etc/httpd/conf/httpd.conf",

        "notify": [

          "restart apache"

        ]

      },

      {

        "name": "Write the default index . html file",

        "template": "src=templates/index . html.j2 dest=/var/www/html/index . html"

      },

      {

        "name": "ensure a pache is running",

        "service": "name=httpd state=started"

      }

    ],

    "handlers": [

      {

        "name": "restart apache",

        "service": "name=httpd state=restarted"

      }

    ]

  }

]

5、ansible常用模块

ansible-doc -l 查看所有的模块,ansible-doc -s 模块名 查看模块的用法。

可以参考http://www.elecfans.com/d/718475.html

ansible命令格式:ansible all -a “/bin/echo hello”

在命令行中 -m 后面接调用模块的名字。-a 后面接调用模块的参数。

如使用模块 copy 复制管理员节点文件/etc/hosts 到所有远程主机/tmp/hosts
 ansible  all  -m  copy  -a  ” src=/etc/hosts  dest=/trnp/hosts ”

使用模块 yum 在远程主机 Web 上安装 httpd 包

ansible  web  -m  yum  -a  ” narne=httpd  state=present”

5.1、常用的模块介绍

调试和测试类的模块:

ping:  ping 一下你的远程主机 , 如果可以通过 Ansible 连接成功,那么返回 pong 。

ansible all -m ping  #检查远程主机是否在线

debug : 用于调试的模块,只是简单打印 些消息 , 有点像 Linux 的 echo 命令。

setup: 远程查看主机的配置信息

ansible test -m setup

文件类的模块:

copy :从本地复制文件到远程节点

ansible all -m copy -a “src=/etc/hosts dest=/tmp/hosts mode=755”

#把/etc/hosts拷贝到/tmp/hosts 目录,权限755

ansible test -m copy -a "src=/etc/hosts dest=/tmp/hello.txt"

#复制到目标后改名字

ansible test -m copy -a "src=/etc/hosts dest=/tmp/a/b/c/"

#复制时会创建目录,包括多级目录

template: 从本地复制文件到远程节点 , 并进行变量的替换。

file :设置文件属性。

ansible test -m file -a "dest=/tmp/dir state=directory" #创建目录
ansible test -m file -a "dest=/tmp/hello state=touch" #创建文件
ansible test -m file -a "dest=/root/test.link state=absent" #删除文件

Linux 上的常用操作。

user :管理用户账户。

ansible all -m user -a "name=hmm password=<fan>"  #添加用户

yum : RedHat 系 Linux 上的包管理。

ansible all -m yum -a “name=软件包名 state=present” #yum安装软件

service :管理服务或启动服务。

ansible all -m service -a “name=httpd state=started” #启动服务

firewalld : 管理防火墙中的服务和端口。

cron: 定时任务

执行 shell 命令。

shell: 在节点上执行 shell 命令,支持$HOME、~\ “〉”、“| ” 、 “ ;”和 “&” 。

ansible all -m shell -a "ps -ef | grep mysql"

command :在远程节点上面执行命令, 默认的模块,不支持$HOME| < >等

ansible all -a "reboot" #重启机器

            raw:和shell差不多

           

            script 在远程执行本地的脚本

     ansible test -m script -a "/sh/test.sh"

6、ansible扩展组件

第二章:ansible进阶

1、ansible的配置

默认配置文件在/etc/ansible目录下的ansible.cfg

参考:

2、主机清单配置

默认的主机清单配置文件:/etc/ansible/hosts

[  ]内是组名,分组也可以支持嵌套;如果有主机的SSH端口不是标准的22端口,可在主机名之后加上端口号,用冒号分隔,SSH 配置文件中列出的端口号不会在 paramiko 连接中使用,会在 openssh 连接中使用.

也可以跟随下面参数

3、ansible的脚本playbook

playbook是ansible的脚本语言,使用的是yaml格式,yaml和json类似,是一种数据表示格式

yaml的写法参考如下:

最基本的playbook脚本分为三个部分

1、在什么机器上以什么身份执行

hosts:

users:

2、执行的任务都有什么

tasks:

3、善后的任务都有什么

handlers:

里面的每一个handler都是对模块的一次调用,而handler与任务不同,任务会默认地按定义顺序执行每一个任务,handler则不会,他需要在任务中被调用,才有可能被执行。在ansible中,只有任务执行状态为changed时,才会执行该任务调用的handler。

什么情况下使用handler呢?

如果你在任务中修改了apchae的配置文件,那么需要重启apache。如果还安装了apache的插件,那么还需要重启apache,像这样的应用场景中,重启apache就可以设计成一个handler

完整的copy模块的代码请参考下面的连接:

https://github.com/ansible/ansible-modules-core/blob/devel/files/copy.py

执行playbook的命令:

ansible-playbook deploy.yml  #执行playbook命令

后面加参数说明:

--verbose 查看输出的细节,

--list-hosts 查看该脚本影响了哪些主机,

-f 10 并行执行脚本

第三章:playbook详解

第四章:Ansible Tower

ansible tower的功能:

1.用户和权限的管理;2.ansible命令行工具能做的事,通过tower网站图形化界面都可以做;3.记录playbook的所有执行结果,提供用户视角和主机视角的统计信息;4.提供了API接口,让第三方平台可以根据API调用Tower的功能。

1、安装ansible tower

推荐使用contos 7 64位操作系统, 最低配置 内存:4G 硬盘:20G

下载网站:https://releases.ansible.com/ansible-tower/setup-bundle/

下载最新包:ansible-tower-setup-bundle-3.3.3-1.el7.tar.gz

tar -zxvf ansible-tower-setup-bundle-3.3.3-1.el7.tar.gz 解压后编辑 inventory里面的admin_password,pg_password, rabbitmq_password,可参考如下图

在安装前主要需要关闭selinux 然后运行 ./setup.sh 开始执行

修改admin的密码:tower-manage changepassword admin (也可以不修改)

安装成功后访问https://IP:80 ,第一次登陆后,会提示你提供licences文件,需要在https://www.ansible.com/license 网站申请密钥,ansible Tower提供免费试用的licences但限制了管理10个一下的主机,而且不能用一些LDAP等高级特性。

原文地址:https://www.cnblogs.com/fan-gx/p/10726751.html