一, ansible 简介
1, 什么是 ansible ?
Ansible 是 2013 年推出的一款 IT 自动化和 DevOps 软件, 目前由 Redhat 已签署 Ansible 收购协议. 其是基于 Python 研发, 糅合了很多老运维工具的优点实现了批量操作系统配置, 批量程序的部署, 批量运行命令等功能.
2,ansible 可以实现:
1)自动化部署 APP
2)自动化管理配置项
3)自动化的持续交付
4)自动化的 (AWS) 云服务管理
3, 为什么要选择 ansible?
)活跃度(社区活跃度)
)学习成本
)使用成本
)编码语言
)性能
)使用是否广泛
ansible 优点:
1)仅需 ssh 和 python 即可使用
2)无需客户端
ansible 缺点:
对于几千台, 上万台机器的操作, 还不清楚性能, 效率情况如何, 需要进一步了解.
4,ansible 特性
1)模块化设计, 调用特定的模块来完成特定任务
2)基于 python 语言实现
- paramiko
- PyYAML (半结构化语言)
- jinja2
3)其模块支持 JSON 等标准输出格式, 可采用任何编程语言重写
4)部署简单
5)主从模式工作
6)支持自定义模块
7)支持 playbook
8)易于使用
9)支持多层部署
10)支持异构 IT 环境
二, ansible 安装
机器准备:
1:ansible 192.168.1.10 管理机器
2:web1 192.168.1.11 托管机器
3:web2 192.168.1.12 托管机器
4:db1 192.168.1.21 托管机器
5:db2 192.168.1.22 托管机器
6:cache 192.168.1.33 托管机器
管理机器:
要求 python2.6 或 python2.7
需要安装 ansible 使用模块:
- paramiko
- PyYAML
- Jinja2
- httplib2
- six
托管机器:
Ansible 默认通过 SSH 协议管理机器
被管理主机要开吭 ssh 服务, 允许 ansible 主机登彔
在托管节点上也需要安装 Python 2.5 或以上的版本
如果托管节点上开启了 SElinux, 需要安装 libselinux-python
1, 在 ansible 管理机器上配置 yum 源
2, 安装 ansible
]#yum -y install ansible
3, 安装完成查看版本信息
]#ansible -version
三, 主机管理
ansible 的配置文件是 ansible.cfg
ansible.cfg 的查找顺序是
1 ANSIBLE_CONFIG 变量定义的配置文件
2 当前目录下的 ./ansible.cfg 文件
3 前用户家目录下 ~/ansible.cfg 文件
4 /etc/ansible/ansible.cfg 文件
ansible.cfg 中 inventony 指定主机分组文件的路径和地址, 默认分组文件 hosts
1, 主机定义于与分组管理 -- 编辑 / etc/ansible/hosts 文件
1.1, 格式:
[组名称]
主机名称或 ip 地址, 登陆用户名, 密码, 端口等信息
1.2, 测试:
ansible [组名称] --list-hosts
2, 主机定义与分组 --inventory 参数说明
1)ansible_ssh_host
将要连接的进程主机名与你想要设定的主机的别名不同的话, 可通过此变量设置.
2)ansible_ssh_port
ssh 端口号, 如果不是默认的端口号, 通过此变量设置.
3)ansible_ssh_user
默认的 ssh 用户名
4)ansible_ssh_pass
ssh 密码(这种方式并不安全, 我们强烈建议使用 --ask-pass 或 SSH 密钥)
5)ansible_sudo_pass
sudo 密码(建议使用 --ask-sudo-pass)
6 ) ansible_sudo_exe (new in version 1.8)
sudo 命令路径(适用亍 1.8 及以上版本)
7)ansible_connection
不主机的连接类型. 比如: local, ssh 或者 paramiko.Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
8)ansible_ssh_private_key_file
ssh 使用的私钥文件, 适用亍有多个密钥, 而你不想使用 SSH 代理的情况
9)ansible_shell_type
目标系统的 shell 类型. 默认情况下, 命令的执行使用'sh'语法, 可设置为'csh' 或'fish'.
10)ansible_python_interpreter
目标主机的 python 路径. 适用于的情况: 系统中有多个 Python, 或者命令路径不是 "/usr/bin/python"
- [web]
- web[1:2]
- [db]
- db1
- db2
- [app:children] # 指定子组
- web
- db
- [app:vars]
- ansible_ssh_user="root"
- ansible_ssh_pass="123456"
- [other]
- cache ansible_ssh_user="root" ansible_ssh_pass="123456"
3, 动态主机
ansible 的 inventony 文件可以是静态也可以是脚本(输出格式 json)
修改 ansible.cfg
- ]#mkdir /var/myansible
- ]#cd /var/myansible
- ]#vim ansible.cfg
- [defaults]
- inventory = urscript
编写 usrscript 脚本
- #!/bin/bash
- echo '
- {
- "web" : ["web1", "web2"],
- "db" : ["db1", "db2"],
- "other" : ["cache"]
- }'
四, 批量执行
1,ansible 命令基础
ansible 主机分组 -m 模块 -a '命令和参数'
- host-pattern 主机戒定义的分组
- -M 指定模块路径
- -m 使用模块, 默认 command 模块
- -a or --args 模块参数
- -i inventory 文件路径, 或可执行脚本
- -k 使用交互式登录密码
- -e 定义变量
- -v 详细信息,-vvvv 开启 debug 模式
2, 列出要执行的主机, 不执行任何操作
]#ansible all -list-hosts
3, 批量检测主机
]#ansible all -m ping
4, 批量执行命令
]#ansible all -m command -a 'id' -k
5, 给所有主机部署密钥
]#ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/id_rsa.pub)'" -k
注意:
报错:"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
解决方法:
修改 ansible.cfg
host_key_checking = False
五, 模块
1) ansible-doc
模块的手册, 相当于 shell 的 man
ansible-doc -l // 列出所有模块
ansible-doc 模块名 // 查看帮助
2) ping 模块
测试网络连通性, ping 模块没有参数(测试 ssh 的连通性)
3) command 模块
默认模块, 远程执行命令没有启用 shell , 所有 shell 相关特性命令无法使用, 例如 <> | &
raw 模块, 没有 chdir create remove 等参数, 能执行大部分操作
shell 模块, 启动 /bin/sh 运行命令, 可以执行所有操作
用法: ansible 主机分组 -m command -a '命令和参数'
测试:
ansible cache -m command -a 'chdir=/tmp touch f1' 创建成功
ansible cache -m shell -a 'chdir=/tmp touch f2' 创建成功
ansible cache -m raw -a 'chdir=/tmp touch f3' 文件可以创建, 但无法切换目录, 文件在用户家目录下生成.
4) shell | raw 模块
shell 模块用法基本和 command 一样, 区别是 shell 模块是通过 / bin/sh 迚行执行命令, 可以执行任意命令
raw 模块, 用法和 shell 模块一样 , 可以执行任意命令
区别是 raw 没有 chdir,creates,removes 参数
5) script 模块
处理复杂操作, 编写脚本
测试:
- #!/bin/bash
- id zhang3
- if [ $? != 0 ];then
- useradd li4
- echo 123456 |passwd --stdin li4
- fi
- ansible all -m scriopt -a "urscriopt.sh"
6) copy 模块
复制文件到进程主机
- src: 要复制到进程主机的文件在本地的地址, 可以是绝对路径, 也可以是相对路径. 如果路径是一个目录, 它将递归复制. 在这种情况下, 如果路径使用 "/" 来结尾, 则只复制目录里的内容, 如果没有使用 "/" 来结尾, 则包含目录在内的整个内容全部复制, 类似于 rsync
- dest: 必选项. 进程主机的绝对路径, 如果源文件是一个目录, 那么该路径也必须是个目录
- backup: 在覆盖之前将原文件备份, 备份文件包含时间信息. 有两个选项: yes|no
- force: 如果目标主机包含该文件, 但内容不同, 如果设置为 yes, 则强制覆盖, 如果为 no, 则只有当目标主机的目标位置不存在该文件时, 才复制. 默认为 yes
测试:
复制文件
]#ansible cache -m copy -a 'src=/root/a.log dest=/root/a.log'
复制目录:
]#ansible cache -m copy -a 'src=urdir dest=/root/'
7) lineinfile | replace 模块
- 类似 sed 的一种行编辑替换模块
- path 目的文件
- regexp 正则表达式
- line 替换后的结果
利用 lineinfile 修改 /etc/sysconfig/network-scriopts/ifcfg-eth0
]#ansible cache -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^ONBOOT"line="ONBOOT=\"no\""
- 替换指定字符
利用 replace 修改 /etc/sysconfig/network-scriopts/ifcfg-eth0
]#ansible cache -m replace -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="^(ONBOOT=).*"replace="\1\"yes\""'
8) yum 模块
- 使用 yum 包管理器来管理软件包
- config_file:yum 的配置文件
- disable_gpg_check: 关闭 gpg_check
- disablerepo: 不启用某个源
- enablerepo: 启用某个源
- name: 要进行操作的软件包的名字, 也可以传递一个 url 或者一个本地的 rpm 包的路径
- state: 状态(present,absent,latest)
删除软件包
]#ansible cache -m yum -a 'name="lftp"state="removed"'
删除多个软件包
]#ansible cache -m yum -a 'name="lftp,lrzsz"state="removed"'
安装软件包
]#ansible cache -m yum -a 'name="lftp" '
安装多个软件包
]#ansible cache -m yum -a 'name="lftp,lrzsz" '
9) service 模块
- name: 必选项, 服务名称
- enabled: 是否开机启动 yes|no
- sleep: 如果执行了 restarted, 在则 stop 和 start 之间沉睡几秒钟
- state: 对当前服务执行启动, 停止, 重启, 重新加载等操作(started,stopped,restarted,reloaded)
测试:
]#ansible cache -m service -a 'name="sshd"enabled="yes"state="started" '
10) setup 模块
- 主要用亍获取主机信息, 在 playbooks 里经常会用到的一个参数 gather_facts 就不该模块相关. setup 模块下经常使用的一个参数是 filter 参数.
- filter 可以过滤到我们需要的信息
]# ansible cache -m setup -a 'filter="ansible_os_family"'
练习:
安装 apache, 修改 apache 监听端口为 8080, 设置默认主页, 启动服务, 设置开机自启.
- ]#ansible web -m yum -a 'name="httpd"']#ansible web -m lineinfile -a'path=/etc/httpd/conf/httpd.conf regexp="^Listen" line="Listen 8080"'
- ]#ansible web -m shell -a 'echo this is test_web> /var/www/html/index.html'
- ]#ansible web -m service -a 'name="httpd"state="restarted"enabled=yes'
来源: http://www.bubuko.com/infodetail-2701923.html