【Ansible学习】-|【Ansible学习】- Ansible初探
安装
yum -y install epel-release
yum -y install ansible
Ansible基础 Ansible架构图
文章图片
Ansible架构图 Ansible核心组件说明
- Ansible:Ansible的核心程序
- Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载
- Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,
剧本
用来定义那些主机需要调用那些模块来完成的功能 - Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用
core Modules
中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。 - Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。
- Connection Plugins:连接插件,Ansible和Host通信使用
Ansible执行过程 清单文件
[servers]
172.20.21.120
172.20.21.121
172.20.21.123
默认清单文件是
/etc/ansible/hosts
。同时也可以在运行时通过
-i
参数来指定清单文件。测试主机连通性
ansible www.baidu.com -m ping
发现执行结果异常,这是因为
ansible
只纳管定义在清单文件中的主机。需要清单文件
ansible servers -m ping
ansible servers -m ping -o
通过
-o
参数,可以让结果在一行中进行显示。测试主机连通性 执行远程指令
ansible servers -m shell -a 'uptime'
ansible servers -m command -a 'uptime'
执行远程指令 远程主机的yum管理 确认软件是否安装
[root@centos7 ~]# ansible servers -m shell -a 'rpm -qa|grep httpd'
[WARNING]: Consider using yum, dnf or zypper module rather than running rpm172.20.21.121 | FAILED | rc=1 >>
non-zero return code
安装httpd
[root@centos7 ~]# ansible servers -m yum -a 'name=httpd state=latest'
172.20.21.121 | SUCCESS => {
"changed": true,
"msg": "Repository base is listed more than once in the configuration\nRepository updates is listed more than once in the configuration\nRepository extras is listed more than once in the configuration\nRepository centosplus is listed more than once in the configuration\nThere are unfinished transactions remaining. You might consider running yum-complete-transaction, or \"yum-complete-transaction --cleanup-only\" and \"yum history redo last\", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).\nThe program yum-complete-transaction is found in the yum-utils package.\n",
"rc": 0,
"results": [
..."
]
}
再次安装
再次安装时,就会检查客户端已安装的组件是否是最新的,若是最新的就不会再次安装。
changed
字段会返回false
。[root@centos7 ~]# ansible servers -m yum -a 'name=httpd state=latest'
172.20.21.121 | SUCCESS => {
"changed": false,
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date",
""
]
}
Ansible组件 - Inventory Inventory主机清单 inventory文件通常用于定义要管理主机的认证信息,例如ssh登录所需的用户名、密码以及key相关信息。
/etc/ansible/hosts
文件配置:[apache]
172.20.21.121
172.20.21.123[nginx]
172.20.21.[120:121]# 把一个组作为另一个组的子成员
[servers:children]
apache
nginx# 定义组变量
[servers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'
[root@centos7 ~]# ansible apache --list-hosts
hosts (2):
172.20.21.121
172.20.21.123
[root@centos7 ~]# ansible nginx --list-hosts
hosts (2):
172.20.21.120
172.20.21.121
[root@centos7 ~]# ansible servers --list-hosts
hosts (3):
172.20.21.121
172.20.21.123
172.20.21.120
[root@centos7 ~]# ansible all -m shell -a 'hostname' -o
172.20.21.123 | SUCCESS | rc=0 | (stdout) centos7
172.20.21.121 | SUCCESS | rc=0 | (stdout) centos7
172.20.21.120 | SUCCESS | rc=0 | (stdout) centos7
Ansible Inventory 内置参数
参数 | 用途 | 例子 |
---|---|---|
ansible_ssh_host | 将要连接的远程主机名ssh地址 | ansible_ssh_host=192.168.1.100 |
ansible_ssh_port | ssh端口 | ansible_ssh_port=3000 |
ansible_ssh_user | ssh 用户名 | ansible_ssh_user=user |
ansible_ssh_pass | ssh 密码 | ansible_ssh_pass=pass |
ansible_sudo | sudo 用户 | |
ansible_sudo_pass | sudo 密码 | |
ansible_sudo_exe | sudo 执行路径 | ansible_sudo_exe=/usr/bin/sudo |
ansible_connection | hosts 连接方式 | ansible_connection=local |
ansible_ssh_private_key_file | hosts 私钥 | ansible_ssh_private_key_file=/root/key |
执行命令 -m shell
[root@centos7 ~]# ansible servers -m shell -a 'hostname' -o
172.20.21.121 | SUCCESS | rc=0 | (stdout) centos7
172.20.21.123 | SUCCESS | rc=0 | (stdout) centos7
172.20.21.120 | SUCCESS | rc=0 | (stdout) centos7
[root@centos7 ~]# ansible servers -m shell -a 'uname -r' -o
172.20.21.121 | SUCCESS | rc=0 | (stdout) 3.10.0-514.26.2.el7.x86_64
172.20.21.123 | SUCCESS | rc=0 | (stdout) 3.10.0-514.26.2.el7.x86_64
172.20.21.120 | SUCCESS | rc=0 | (stdout) 3.10.0-514.26.2.el7.x86_64
复制文件 -m copy
ansible all -m copy -a "src=https://www.it610.com/article/test.sh dest=/tmp"
复制文件 软件包管理 -m yum
ansible apache -m yum -a "name=vim state=present"
软件包安装
ansible apache -m yum -a 'name=httpd state=absent'
软件包卸载 服务管理 -m service
ansible apache -m service -a "name=httpd state=restarted enabled=yes"
服务管理 Ansible组件 - Facts facts组件是Ansible用于采集被管理主机信息的功能,可以使用
setup
模块查看主机所有的facts信息。可以使用filter
参数对结果进行过滤。ansible apache -m setup
ansible apache -m setup -a "filter=ansible_default_ipv4"
文章图片
setup模块 Ansible组件 - playbook
playbook
是有一个或者多个play
组成的列表。play
的主要功能在于将实现归并为一组的主机装扮成实现通过ansible中的task
定义好的角色role
。根本上来讲,所谓的
task
无非是调用ansible的一个module
。将多个play
组织在一个playbook
中,即可以让他们联合起来按事先编排的机制完成某一任务。文章图片
playbook结构 playbook示例 yaml剧本
- hosts: apache
tasks:
- name: Install httpd
yum: name=httpd state=present- name: copy httpd conf
template: src=https://www.it610.com/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
owner=root group=root mode=0644
notify:
- Restart Apache Service- name: enable httpd is runing
service: name=httpd state=started enabled=yeshandlers:
- name: Restart Apache Service
service: name=httpd state=restarted
检查
ansible-playbook apapche.yaml --syntax-check
ansible-playbook apapche.yaml --list-task
ansible-playbook apapche.yaml --list-hosts
执行playbook
ansible-playbook apapche.yaml
执行playbook 【【Ansible学习】-|【Ansible学习】- Ansible初探】如果觉得有用,欢迎关注我的微信,有问题可以直接交流:
文章图片
你的关注是对我最大的鼓励!
推荐阅读
- 宽容谁
- 我要做大厨
- 增长黑客的海盗法则
- 画画吗()
- 2019-02-13——今天谈梦想()
- 远去的风筝
- 三十年后的广场舞大爷
- 叙述作文
- 20190302|20190302 复盘翻盘
- 学无止境,人生还很长