ansible 管理 以及 playbook的说明

ansible 管理 以及 playbook的说明
20180901 陈信
20190904 更新
官网: https://docs.ansible.com/ansible/latest/user_guide/quickstart.html

说明
Ansible是一种自动化的运维工具,基于Python开发,它集合了众多运维工具(比如puppet、chef、func等)的优点,能够实现批量操作。但其实Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,Ansible只是提供一种框架.

  • Ansible 是个与 Puppet, SaltStack, Chef 并驾齐驱的组态设定 (Infrastructure as Code) 工具,其简单易用的特性让人爱不释手,在 DevOps 界更佔有一席之地。
  • Ansible 提供一种最简单的方式用于发布、管理和编排计算机系统的工具,你可在数分钟内搞定。
  • Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。

Ansible特性:

  • 拥有模块化的设计,Ansible能够调用特定的模块来完成特定任务 ,本身是核心组件,短小精悍 ;
  • Ansible是基于Python语言实现的,由Paramiko (python 的一个可并发连接 ssh 主机功能库 ) , PyYAML和Jinja2 ( 模板化 ) 三个关键模块实现;
  • Ansible的部署比较简单,agentless 无客户端工具;
  • 以主从模式工作;
  • 支持自定义模块功能;
  • 支持playbook剧本,连续任务按先后设置顺序完成;
  • 期望每个命令具有幂等性:

安装
安装常用两种方式,pip程序安装和yum安装

安装依赖包
yum install gcc
yum install python27-devel
yum install openssl-devel
yum install pip

pip版本
[root@ip-10-0-1-23 ansible]# pip --version
pip 18.0 from /usr/lib/python2.7/dist-packages/pip (python 2.7)

安装ansible
pip install ansible
[root@ip-10-0-1-23 ansible]# ansible --version
ansible 2.6.3
config file = None
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible #主要为这个文件夹
executable location = /usr/bin/ansible
python version = 2.7.14 (default, May 2 2018, 18:31:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

yum版本
也可以yum install ansible

ansible文件目录结构
配置文件目录:/etc/ansible/
执行文件目录:/usr/bin/
Lib库依赖目录:/usr/lib/pythonX.X/site-packages/ansible/ #参考示例,实际情况未必

[root@ip-10-0-1-23 dist-packages]# ls /usr/lib/python2.7/dist-packages/ansible*
ansible/ 模块文件和py文件,如下
[root@ip-10-0-1-23 ansible]# ls /usr/lib/python2.7/dist-packages/ansible/modules/
cloud commands database identity init.pyc messaging net_tools notification remote_management storage utilities windows
clustering crypto files init.py inventory monitoring network packaging source_control system web_infrastructure
ansible-2.6.3.egg-info/ 其他文件

配置与执行
ansible配置文件的查找顺序
检查环境变量ANSIBLE_CONFIG指向的路径文件(export ANSIBLE_CONFIG=/etc/ansible.cfg )
~/.ansible.cfg,检查当前目录(/etc/ansible)下的ansible.cfg配置文件
/etc/ansible.cfg 检查etc目录的配置文件

ansible配置文件
设置/etc/ansible/ansible.cfg配置参数,ansible有许多参数,下面列出常用的参数(略).参考 https://blog.csdn.net/AhhSong/article/details/78757664
因pip安装后,并无此文件.后期使用palybook的时候,可能会用到这个.

制作主机配置文件
mkdir /etc/ansible
[root@ip-10-0-1-23 ansible]# cat /etc/ansible/hosts
[deployServer] #组名称,可根据需求定义
10.0.1.198:4399

配置密钥
[web@ip-10-0-1-23 .ssh]$ pwd
/home/web/.ssh
[web@ip-10-0-1-23 .ssh]$ ls
authorized_keys id_dsa known_hosts

执行命令,完成远程调用
[web@ip-10-0-1-23 .ssh]$ ansible deployServer -m command -a "sudo ls -al /root/.ssh" #sudo不推荐,推荐become方式.
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
10.0.1.198 | SUCCESS | rc=0 >>
total 12
-rw------- 1 root root 560 Jun 21 15:12 authorized_keys

关于ssh登陆主机,提示连接让输入yes/no信息以便写入known_hosts文件的取消问题说明
如果/etc/ssh/ssh_config 文件配置了 StrictHostKeyChecking no 和 UserKnownHostsFile /dev/null ,切换到web用户(普通用户)后,不生效,则检查看ssh_config文件是否有web的可读取权限.如果权限是600,则web用户无法读取该文件,就会造成执行命令(遇到新加入的主机)需要用户确认该主机是否连接正确的提示.
-rw-r--r-- 1 root root 423 Jun 22 17:26 ssh_config

命令方式说明
入门示例
ansible deployServer -m command -a "sudo ls -al /root/.ssh" #sudo方式虽然暂时仍支持,但已经不推荐使用(后期版本可能会取消).推荐become方式.
远程执行本地SHELL脚本(类似scp+shell)
echo "date" > t.sh; ansible deployServer -m script -a "t.sh"

命令参数说明
示例
[web@ip-10-0-1-23 ~]$ ansible deployServer -b --become-user=root -m command -a "ls /root/"
[web@ip-10-0-1-23 ~]$ ansible deployServer -u web -b --become-user=root -i /etc/ansible/hosts -m command -a "ls /root/"
[web@ip-10-0-1-23 ~]$ ansible deployServer -u web -b --become-user=root -i /etc/ansible/hosts -o -m command -a "ls /root/"

格式
ansible [options]

连接选项
-u REMOTE_USER, --user=REMOTE_USER 以此用户的密钥去连接远方
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE 指定使用的密钥
-T TIMEOUT, --timeout=TIMEOUT 默认连接超时为10s

特权升级选项
[web@ip-10-0-1-23 ~]$ ansible deployServer -m command -a "ls /root/" -b --become-user=root #web用户可以sudo为root
-b, --become 以become方式来运行
--become-method=BECOME_METHOD 特权升级方式 (default=sudo),可选[ sudo | su |...|enable | machinectl ]
--become-user=BECOME_USER 以此用户身份运行(default=root)

一般参数(涉及到具体命令的参数)
-m MODULE_NAME 调用模块
-a MODULE_ARGS 模块的参数
-i INVENTORY, --inventory=INVENTORY 指定目标主机路径
--list-hosts 只列出将会操作的主机,不会执行任何指令
-f FORKS, --forks=FORKS 并行任务数.默认是5
-o, --one-line 每个主机操作后的输出放在1行里
--playbook-dir=BASEDIR playbook目录
--syntax-check 检查playbook语法,但不执行

检测类命令参考: https://medium.com/laraveldojo/讓您安心執行-ansible-playbook-的小技巧-1-c634f2963bc9
--syntax-check
--list-tasks
--list-hosts
--check
--diff
--step

其他模块说明(除命令模块外)
copy模块
实现主控端向目标主机拷贝文件, 类似scp功能

ansible deployServer -m copy -a "src=~/test.sh dest=/tmp/ owner=root group=root mode=0755"

shell模块
shell模块在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等 :
[root@CentOS7-master ~]# ansible webservers -m shell -a 'cat /etc/passwd |grep "root"'

stat模块
获取远程文件状态信息, 包括atime, ctime, mtime, md5, uid, gid等信息

ansible deployServer -m stat -a "path=/etc/sysctl.conf"

get_url模块
实现在远程主机下载指定URL到本地

ansible deployServer -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0400 force=yes"

cron模块
远程主机crontab配置.cron管理cron计划任务

ansible deployServer -m cron -a "name='check dir' hour='5,2' job='ls -alh > /dev/null'"

service模块
远程主机系统服务管理

ansible deployServer -m service -a "name=crond state=stopped"

ansible deployServer -m service -a "name=crond state=restarted"

ansible deployServer -m service -a "name=crond state=reloaded"

user服务模块
远程主机系统用户管理
添加用户:

ansible deployServer -m user -a "name=deploy comment='test'"

删除用户:

ansible deployServer -m user -a "name=deploy state=absent remove=yes"

yum安装软件

其他命令说明
ansible命令集

/usr/bin/ansible # Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行
/usr/bin/ansible-doc # ansible 模块功能查看工具
/usr/bin/ansible-galaxy # 下载/上传优秀代码或Roles模块 的官网平台,基于网络的
/usr/bin/ansible-playbook # ansible 定制自动化的任务集编排工具
/usr/bin/ansible-pull # ansible远程执行命令的工具,拉取配置而非推送配置(使用较少,海量机器时使用,对运维的架构能力要求较高)
/usr/bin/ansible-vault # ansible 文件加密工具
/usr/bin/ansible-console # ansible基于Linux Consoble界面可与用户交互的命令执行工具

ansible 2.6.3版本还多了以下3个:
/usr/bin/ansible-config -> ansible
/usr/bin/ansible-connection
/usr/bin/ansible-inventory -> ansible

playbook
参考:
http://www.361way.com/ansible-playbook-example/4441.html palybook的使用和说明(比较老的版本,2015)
https://ansible-tran.readthedocs.io/en/latest/docs/playbooks_intro.html ansible中文权威指南

playbook的构成
playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中即可以让它们联同起来按事先编排的机制同唱一台大戏。其主要有以下四部分构成

  1. playbooks组成:
  2. Target section: 定义将要执行 playbook 的远程主机组
  3. Variable section: 定义 playbook 运行时需要使用的变量
  4. Task section: 定义将要在远程主机上执行的任务列表
  5. Handler section: 定义 task 执行完成以后需要调用的任务

不打算采用palybook和ansible的说明:
目前来看,playbook可以执行比较大型的shell任务.但需要熟悉每个yml配置文件的编写模式.还不如直接采用脚本调用,剔除ansible,直接采用自己的方式,反而效率和完整性,安全性会更好一些.同时,也减少了编写不同yml配置文件的时间和知识成本.(目前来看,这个想法是错误的.原因是通过不同格式和知识的积累,会发现很多通用的东西.2019/06/30).

posted @ 2020-04-21 14:40  ChanixChen  阅读(551)  评论(0编辑  收藏  举报