核心概念

中文官网

官网

官网2.6版本

一、架构


image

组件:

  • Inventory:Ansible 管理的主机信息,包括 IP 地址、SSH 端口、账号、密码等;
  • Modules:任务均有模块完成,也可以自定义模块,例如经常用的脚本;
  • Plugins:使用插件增加 Ansible 核心功能,自身提供了很多插件,也可以自定义插件。例如 connection 插件,用于连接目标主机。callback 插件可以将结果输出到其他地方。vars 插件将定义的变量注入到 Ansible 中运行
  • Playbooks:“剧本”,模块化定义一系列任务,供外部统一调用。Ansible 核心功能

二、安装与配置

参考链接

##### 1、安装
[root@slave-1 ~]# yum install ansible
##### 2、常用配置项
# vi /etc/ansible/ansible.cfg 
[defaults]
inventory = /etc/ansible/hosts
forks = 5   //并发数
sudo_user = root
remote_port = 22
host_key_checking = False
timeout = 10
log_path = /var/log/ansible.log
private_key_file = /root/.ssh/id_rsa
##### 3、主机与组
[root@slave-1 ansible]# vi /etc/ansible/hosts
192.168.2.62 ansible_ssh_user=root ansible_ssh_pass=123456
[webservers]
192.168.2.63 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.2.64 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.2.65 ansible_ssh_user=root  ansible_ssh_key=/root/.ssh/id_rsa

##### 4、定义变量
[webservers:vars] 
ntp_server=ntp.example.com 
proxy=proxy.example.com

vi /etc/ansible/group_vars/webservers.yml
work_dir: /data/nginx

三、ad-hoc模式

参考链接

[root@slave-1 ansible]# ansible webservers -m command -a "df -h"
[root@slave-1 ansible]# ansible webservers -a "df -h"
[root@slave-1 ansible]# ansible all -a "df -h"
[root@slave-1 ansible]# ansible webservers -a "echo {{ansible_ssh_pass}}"
[root@slave-1 ~]# ansible all -m setup

##### 1、Parallelism and Shell Commands
$ ansible atlanta -a "/usr/bin/foo" -u username
$ ansible raleigh -m shell -a 'echo $TERM'

##### 2、File Transfer
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

##### 3、Managing Packages
$ ansible webservers -m yum -a "name=acme state=present"
$ ansible webservers -m yum -a "name=acme state=absent"

##### 4、Users and Groups
$ ansible all -m user -a "name=foo password=<crypted password here>"
$ ansible all -m user -a "name=foo state=absent"

##### 5、Deploying From Source Control
$ ansible webservers -m git -a "repo=https://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

##### 6、Managing Services
$ ansible webservers -m service -a "name=httpd state=started"

posted @ 2022-02-23 10:56  曾某某scau  阅读(121)  评论(0编辑  收藏  举报