ansible

1.ansible简介

  • ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansible

  • ansible执行命令、脚本的时候会通过SSH连接远程主机

  • ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

  • ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

    • (1)、连接插件connection plugins:负责和被监控端实现通信;
    • (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
    • (3)、各种模块核心模块、command模块、自定义模块;
    • (4)、借助于插件完成记录日志邮件等功能;
    • (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
  • ansible的架构:连接其他主机默认使用ssh协议

在这里插入图片描述

2.ansible部署

IP地址
管理端192.168.100.21
被管理端192.168.100.22
被管理端192.168.100.23

管理端

yum -y install epel-release		//安装ansible
yum -y install ansible

yum -y install tree
tree /etc/ansible/				//查看ansible版本
/etc/ansible/
├── ansible.cfg
├── hosts
└── roles

cd /etc/ansible/
[web]
192.168.100.22
[mysql]
192.168.100.23


ssh-keygen -t rsa
ssh-copy-id root@192.168.100.22
ssh-copy-id root@192.168.100.23		//配置密钥对验证

ssh-agent bash		//免交互
ssh-add

3.ansible命令行模式

3.1command模块

命令格式:ansible [主机] [-m 模块] [-a args]
ansible-doc -l		//列出所有已安装的模块(按q退出)
ansible-doc -s yum	//-s列出yum模块描述信息和操作动作

ansible web -m command -a 'date'	//web在hosts文件中,指定分类执行
ansible 192.168.100.22 -m command -a 'date'		//指定IP执行
ansible mysql -m command -a 'date'
ansible all -m command -a 'date'		//所有hosts主机执行date文件
ansible all -a 'ls /'		//如果不加-m模块,则默认运行command模块

3.2cron模块

两种状态(state):present表示添加(可以省略),absent表示移除
ansible-doc -s cron		//查看cron模块信息
ansible web -m cron -a 'minute="*/1" job="/usr/bin/echo hello >> /opt/hello.txt" name="test_hello"'
ansible web -a 'crontab -l'
ansible web -m cron -a 'name="test_hello" state=absent'		//移除计划任务,假如该计划任务没有取名字,name=None即可

3.3user模块

user模块是请求的是useradd,userdel,usermod三个指令
ansible-doc -s user
ansible web -m user -a 'name="test"'	//创建用户test
ansible web -a 'tail /etc/passwd'
ansible web -m user -a 'name="test" state=absent'	//删除用户test

3.4group模块

group模块请求的是useradd,userdel,usermod三个指令
ansible-doc -s group
ansible web -m group -a 'name="testgroup" gid=1020 system=yes'
ansible web -a 'tail /etc/group'
ansible web -m user -a 'name=testuser uid=1020 system=yes group=testgroup'
ansible web -a 'tail /etc/passwd'
ansible web -a 'id testuser'

3.5copy模块

ansible-doc -s copy
ansible web -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
ansible web -a 'ls -l /opt'
ansible web -a 'cat /opt/fstab.back'

ansible web -m copy -a 'content="hello!" dest=/opt/fstab.back'	//将hello!写入/opt/fstab.back
ansible web -a 'cat /opt/fstab.back'

3.6file模块

ansible-doc -s file
ansible web -m user -a 'name=mysql system=yes'
ansible web -m group -a 'name=mysql system=yes'
ansible web -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'	//修改文件的属主属组权限等
ansible web -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'			//设置链接文件
ansible web -m file -a "path=/opt/fstab.back state=absent"	//删除文件
ansible web -m file -a "path=/opt/test state=touch"			//建立文件
ansible web -m file -a 'path=/opt/temp state=directory mode=755'	//创建目录

3.7ping模块

ansible all -m ping
192.168.100.22 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.100.23 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

3.8yum模块

ansible-doc -s yum 
ansible web -m yum -a 'name=httpd'		//安装httpd
[root@server ~]# rpm -q httpd

ansible web -m yum -a 'name=httpd state=absent'		//卸载httpd
[root@server ~]# rpm -q httpd

3.9service模块

ansible-doc -s service
[root@server ~]# yum -y install httpd
ansible web -a 'systemctl status httpd'	//查看httpd运行状态
ansible web -m service -a 'enabled=true name=httpd state=started'	//启动httpd服务
ansible all -m service -a 'name=firewalld state=stopped'	//关闭防火墙
[root@server ~]# systemctl status httpd

3.10shell模块

chdir:指定工作目录,在执行对应的命令之前,会先进入到chdir参数指定的目录中
creates:指定一个文件,当执行的文件存在时,就不执行对应命令
removes:使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令
ansible-doc -s shell
ansible web -m shell -a 'chdir=/opt/share echo hello > hello.txt'	//使用command模块不识别重定向符号
ansible web -m shell -a 'chdir=/opt/share ls'

3.11script模块

ansible-doc -s script
vi test.sh	//在ansible管理端创建脚本
#!/bin/bash
echo "hello ansible from script"> /opt/script.txt

chmod +x test.sh
ansible web -m script -a 'test.sh'
[root@server ~]# cat /opt/script.txt

3.12setup模块

ansible-doc -s setup
ansible web -m setup	//获取web组主机的facts信息

4.主机清单

  • ansible默认的主机清单是/etc/ansible/hosts文件
  • 主机清单可以手动设置,也可以通过Dynamic Invention动态生成
  • 一般主机名使用FQDN
url		http://www.baidu.com:80/new
		协议://主机名.二集域名.顶级域名:端口/虚拟目录
FQDN	完全合格域名		www.baidu.com
域名		baidu.com
  • hosts文件详解
vi /etc/ansible/hosts
[webserver]		//中括号设置组名
www1.example.org	//定义被监控主机,这边可以是主机名也可以是IP地址,主机名需要修改/etc/hosts文件
www2.example.org:2222	//冒号后定义远程连接端口,默认是ssh的22的端口

//如果是名称类似的主机,可以使用列表的方式标识各个主机
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456

[dbbservers]
db-[a:f].example.org	//支持匹配a b c ...f

下面是Inventory中变量
(1)主机变量
[webserver]
www1.magedu.com http_port=80 maxRequestsChild=808
www2.magedu.com http_port=8080 maxRequestsChild=909
(2)组变量
[servers:vars]
ntp_server=ntp.example.org
nfs_server=nfs.example.org
(3)组嵌套
[apache]
http1.example.org
http2.example.org

[nginx]
ngx1.example.org
ngx2.example.org

[webservers:children]
apache
nginx
  • inventory变量参数
参数说明
ansible_ssh_host将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置
ansible_ssh_portssh端口号.如果不是默认的端口号,通过此变量设置
ansible_ssh_user默认的 ssh 用户名
ansible_ssh_passssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_ssh_private_key_filessh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况
ansible_ssh_common_args此设置附加到sftp,scp和ssh的缺省命令行
ansible_sftp_extra_args此设置附加到默认sftp命令行
ansible_scp_extra_args此设置附加到默认scp命令行
ansible_ssh_extra_args此设置附加到默认ssh命令行
ansible_ssh_pipelining确定是否使用SSH管道。 这可以覆盖ansible.cfg中得设置
ansible_shell_type目标系统的shell类型.默认情况下,命令的执行使用 ‘sh’ 语法,可设置为 ‘csh’ 或 ‘fish’
ansible_python_interpreter目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python
ansible_*_interpreter这里的"*"可以是ruby 或perl 或其他语言的解释器,作用和ansible_python_interpreter 类似
ansible_shell_executable这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值