您的位置:首页 > 其它

第三十六课 ansible基本应用和corosync群集基础及安装配置

2016-11-27 16:21 465 查看
一、linux自动化运维
linux 自动化运维大致分三种场景:操作系统安装、系统配置、应用程序部署。


OS provisioning 操作系统的安装:PXE 、cobbler

OS configure 操作系统的配置:cfengine、puppet、saltstack、chef
Deployment 应用程序的部署:funs(ssl)、fabric(ssh)、ansible


1、ansible的安装及配置文件介绍

2、基本应用 官方站点:www.ansible.com/home,在epel源中有ansible,是Python开发
命令格式:ansible <host pattern 主机模式> [-m module 模块] -a 'module_args'


常用模块:command user copy cron file filesystem group hostname ping redis yum sysctl shell script service

3、定义主机组/etc/ansible/hosts,一台主机可以属于多个组

[hbhosts]
192.168.1.10
192.168.1.11
4、模块应用实例 #查看磁盘使用情况
ansible hbhosts -m command -a 'df -h' -k
-k 基于口令访问
如果不想root用户登录,可以编辑hosts文件如
[hbhosts]

192.168.1.10 ansible_ssh_pass=123456 ansible_ssh_user=user1 ansible_ssh_port=999

192.168.1.11 ansible_ssh_pass=123456 ansible_ssh_user=user1 ansible_ssh_port=999

#2台机器同时创建fedora 用户

ansible hbhosts -m command -a 'useradd fedora'


ansible-doc -l 列出ansible的模块

ansible-doc user 查看user模块的用法,比如给用户创建密码

创建用户,添加密码
openssl passwd -1 -salt `openssl rand -hex 4`

passwd:
$12l3kjkidf5ll%kjklj
ansible bhhosts -m user -a 'name=fedora password=$12l3kjkidf5ll%kjklj'

#copy 模块,复制文件到2台目标主机

ansible hbhosts -m copy -a 'src=/root/haha.rpm dest=/tmp/'

可以owner=xiaoliu group=xiaoliu mode=0655 指定属主、组、权限,默认也复制是的为准


#cron模块、同步2台服务器时间

ansible hbhosts -m cron -a 'name="sync time" minute="*/3" job="/usr/sbin/ntpdate 127.0.0.1"'

#从远程主机复制文件到本地主机,如果远程主机文件不一致就有麻烦了,通过变量来指定

ansible bhhosts -m fetch -a 'src=/tmp/somefile dest=/tmp/ flat=yes'

#创建远程链接和指定文件路劲\属主、权限等
ansible hbhosts -m file -a 'path=/etc/foo.conf owner=foo group=foo mode=664'

ansible hbhosts -m file -a 'path=/tmp/test.txt state=touch' 创建空文件test.txt

#ping 主机是否在线

ansible hbhosts -m ping

#yum 安装程序包

ansible hbhosts -m yum -a 'name=vsftpd state=present/remove' 指定状态为安装或删除

ansible hbhosts -m yum -a 'name="@Development tools" stat=present'

#service 控制启动vsftpd服务并开机自动启动

ansible hbhosts -m service -a 'name=vsftpd state=started enabled=yes'

#shell 模块给用户加密码

ansible hbhosts -m shell -a 'echo 123456 | passwd --stdin fedora'

shell把后面的命令当脚本来执行


#script,先创建一个脚本,后ansible 加载脚本

vi useradd.sh

#!/bin/sh

for i in {1..10};do

useradd user$i

echo mageedu | passwd --stdin user$i &> /dev/null

done

ansible hbhosts -m script -a '/root/useradd.sh'


获取模块:ansible-doc -l
ansible-doc module_name


5、基于密钥认证
#ssh-keygen -t rsa -P '' #生成密钥对
#ansible hbhosts -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys owner=root group=root mode=600' #把公钥复制的每一个主机上
#ssh 192.168.1.10 'date' 测试公钥复制生效,然后编辑hosts文件,删除其他选项只留 IP地址,如:
[hbhosts]
192.168.1.10
192.168.1.11

或者#ssh-keygen
#ssh_copy_id /root/.ssh/id_rsa.pub root@客户端ip地址


6、ansible.cfg 主配置文件(修改一批管理多少主机可以在此设置,默认一次修改5台主机)

安装实例:
ansible只需安装在一台控制器上,来管理其他node1、node2、node3 等
我们现在找一台控制器来安装ansible。

安装epel源
#rpm -ivh http://download.fedoraprojet.org/pub/epel/6/x86_64/epel- release-6.8.noarch.rpm


yum install ansible -y

rpm -ql ansible | less#查看ansible的安装路径
ansible -h #查看帮助
vi /etc/ansible/hosts ,删除多余说明文件,定义主机组,如
【hbhost】
192.168.100.7
192.168.100.8
ansible hbhosts -m command -a 'date' -k #查看定义主机组的日期,以ssh链接,基于口令验证(不加-k基于秘钥验证),第一次要输入密码。
ansible hbhosts -m command -a 'df -h' -k #查看2台主机的磁盘空间

如果不想输入密码登录或使用普通用户或不想使用默认端口号,还可以将密码写入hosts配置文件中,如
[hbhosts]

192.168.1.10 ansible_ssh_pass=123456 ansible_ssh_user=user1 ansible_ssh_port=999

192.168.1.11 ansible_ssh_pass=123456 ansible_ssh_user=user1 ansible_ssh_port=999
各模块的参数可以ansible-doc -l 或ansible-doc mod_name 来查看帮助信息


二、ansible playbooks:
1、 ansible 基本机构:

Inventory:清单、列表;定义被管控的主机、主机组

Modules :模块,定义对被管控主机执行的命令

Ad Hoc Commands:对被管控主机直接执行命令

Playbooks:批量管理被控主机的脚本

Tasks:任务,由各模块所支持执行的特定操作;
-m user -a 'name= password='

variables:变量,控制语法结构
templates:模板,实现不同配置文件的文本文件模板,使用模板语言来定义
handlers:处理器,事先定义好的可以在某些条件下被触发的操作
roles:角色,层次型组织playbook及其所依赖的各种资源的一种机制:可被单独调用,

如不同功能(web\db\cache)服务器执行相同的操作(ntptime 同步时间)。


2、剧本案例,node1、node2节点上安装httpd服务并开机启动。

#vim web.yaml

- name: web servers #定义一个剧本的功能

remote_user: root #运行剧本的用户

#sudo: true #如果定义普通用户,需要定义sudo来执行某些权限

hosts: hbhosts #剧本对象列表

tasks: #剧本具体的任务

- name: install httpd #第一个任务安装httpd 服务

yum: name=httpd state=present #模块名称及参数

- name: httpd service #第二个任务启动httpd 服务

service: name=httpd enabled=yes state=started #模块名称及参数
#ansible-playbook web.yaml


3、安装vsftpd服务并开机启动
#!/bin/bash
#this scripts is reinstall httpd for liwenjia
- name: install vsftp
hosts: webhost
user: root
tasks:
- name: install vsftp
yum: name=vsftpd state=present
- name: service vsftpd
service: name=vsftpd enabled=yes state=started

三、ansible 基础元素


1、变量

a.变量的命名仅能字母、数字、下划线组成,只能字母开头

b.facts 是由正在通信的远程目标主机发回的信息,被保存在ansible变量中,如

#ansible hostname -m setup

2、inventory 主机列表,默认文件为/etc/ansible/hosts,iventory file 可以有多个,也可以通过Dynamic inventory 动态生成。

a.文件格式:

【webserver】

192.168.1.111

www.baidu.com #可以是域名,但要能解析
192.168.1.222:8080 #可以自己定义端口号

【webservers】

www[01:05].example.com #遵循相似的命名规则,还可以使用列表的方式标示各主机

db-[a:f].example.com

3、主机变量

可以在inventory中定义主机时为其添加主机变量便于在playbook中使用,如

【webserver】

www1.baidu.com http_port=80 maxRequestsPerChild=808

www2.baidu.com http_port=8080 maxRequestsPerChild=909
4、组变量

指赋给指定组内所有主机上在playbook中可用的变量,如

【webservers】

www1.magedu.com

www2.magedu.com

【webserver:vars】 #固定格式,前面定义的每个主机都执行此变量

ntp_server=ntp.magedu.com

nfs_server=nfs.magedu.com

5、组嵌套

指组中还可以包含其他组,并且也可以向组中的主机指定变量,不过这些变量不支持ansible,只能在ansible-playbook中使用。如:

【apache】

www1.magedu.com

www2.magedu.com

【nginx】

ngx1.magedu.com

ngx2.magedu.com

【webservers:children】 #嵌套组

apache

nginx


6、inventory参数,ansible基于ssh链接远程主机时,可以通过参数指定其交互方式,

参数如下:

ansible_ssh_host

ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass ansible_sudo_pass
ansible_connection #连接主机的方式,默认paramiko ansible_ssh_private_key_file #指定不同远程主机使用私钥文件
ansible_shell_type ansible_python_interpreter #python解释器的版本
四、条件测试
1、when语句

在task后添加when子句即可使用条件测试:when语句支持jinja2表达式语法:例如

task:

- name:"shutdown Debian flauored systems"

command:/sbin/shutdown -h now

when:ansible_os_family == "Debian"


五、迭代
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句来指明迭代的元素列表即可,
如1:

-name:add users
remote_user:root

hosts:hbhosts

tasks:

- name:add serveral users

user:name=`item` state=present groups=wheel

with_items: ###present 表示创建用户

- testuser1

- testuser2

等同于:

- name:add user testuser1

user:name=testuser1 state=present groups=wheel


- name:add user testuser1
user:name=testuser1 state=present groups=wheel 事实上,with_items中可以使用元素还可为hashes, 如下: - name:add several users
user:name=`item`.`name` state=present group=`item`.`groups`
with_items:
- { name: 'testuser1',groups:'wheel' }
- { name: 'testuser2',groups: 'root' }
ansible的循环机制还有好多高级功能.
#ansible-playbooks adduser.yaml,查看结果,用户已经成功添加。
如2:vim zsh.yaml
- name install zsh
remote_user:root
hosts:hbhosts
tasks:
- name:install zsh
yum:name=zsh state=present
when:ansible_pkg_mgr == "yum"
#ansible-playbook zsh.yml,查看客户端主机安装的zsh
六、ansible playbooks 例如: - name:web service
remote_user:root hosts:hbhosts
tasks:
- include: /opt/tasks/main.yml #把任务定义在另一个配置文件中 handlers: - include:/opt/handlers/main.yml
[b]/opt/tasks/main.yml ,如下:[/b][b] - name:install httpd
[/b][b] yum:name=httpd state=latest
[/b][b] tags:install
[/b][b] - name:configration file
[/b][b] copy:src=/opt/files/httpd.conf dest=/etc/httpd/conf/httpd.conf
[/b] tags:conf
notify:restart httpd
- name:start httpd
service:name=httpd enabled=yes state=started
/opt/handlers/main.yml ,如下:
- name:restart httpd
service:name=httpd state=restarted
#absible-playbook httpd.yml ,查看结果,这样就把一个大的文件分割成多个小的配置文件来实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ansible
相关文章推荐