Ansible ad-hoc

ad-hoc
主机管理
批量执行
主机定义与分组
动态主机
ansible命令基础
批量部署证书文件

ad-hoc

主机管理

主机定义与分组

  • 安装ansible之后可以做一-些简单的任务
  • ansible配置:文件查找顺序
    首先检测ANSIBLE_CONFIG变量定义的配置文件
    其次检查当前目录下的 ./ansible.cfg 文件
    再次检查当前用户家目录下 ~/ansible.cfg文件
    最后检查/etc/ansible/ansible.cfg文件
    /etc/ansible/ansible.cfg是ansible的默认配置文件路径
[root@web1 ~]# vim /etc/ansible/ansible.cfg
inventory      = /etc/ansible/hosts		# 14行 把他的注释去掉即可
				# 主机文件的路径(/etc/ansible/hosts)
host_key_checking = False				# 61行 把他的注释去掉即可
				# 不需要管理主机的ssh的key(也就是yes)

主机定义与分组( 续1 )

  • ansible.cfg配置文件
    inventory定义托管主机地址配置文件
    先编辑/etc/ansible/hosts文件,写入远程主机的地址
  • 格式
#表示注释
[组名称]
主机名称或ip地址,登录用户名,密码、端等信息
[root@web1 ~]# vim /etc/ansible/hosts
.. .. .. .. .. ..
## db-[99:101]-node.example.com
# web[1:9]这样也可以的
[web]
web1
web2

[mysql]
mysql1
mysql2

[ui]
xl
  • 测试
[root@web1 ~]# ansible [组名称] --list-hosts
[root@web1 ~]# ansible web --list-hosts		# 查看刚刚定义的web有几台主机
  hosts (2):
    web1
    web2
[root@web1 ~]# ansible mysql --list-hosts
  hosts (2):
    mysql1
    mysql2
[root@web1 ~]# ansible ui --list-hosts
  hosts (1):
    xl
[root@web1 ~]# ansible web,mysql --list-hosts
  hosts (4):
    web1
    web2
    mysql1
    mysql2
[root@web1 ~]# ansible web1,mysql2 --list-hosts
  hosts (2):
    web1
    mysql2
[root@web1 ~]# ansible all --list-hosts		# all(所有)
  hosts (5):
    web1
    web2
    xl
    mysql1
    mysql2

主机定义与分组( 续2 )

  • inventory参数说明

ansible [组名称] -m ping -k
定义主机,分组和子组

[root@mysql1 ~]# vim /etc/ssh/sshd_config
Port 2222			# 17行	端口
[root@mysql1 ~]# firewall-cmd --zone=public --add-port=2222/tcp --permanent 
success
[root@mysql1 ~]# firewall-cmd --reload
success
[root@mysql1 ~]# firewall-cmd --zone=public --query-port=2222/tcp
yes
[root@mysql1 ~]# setenforce 0
[root@mysql1 ~]# systemctl restart sshd
[root@mysql1 ~]# ss -untlp | grep 2222
[root@web1 ~]# ssh -p 2222 mysql1
root@mysql1's password: 
Last login: Sat Mar 14 22:05:38 2020 from 192.168.199.1
[root@mysql1 ~]# 
[root@web2 ~]# ssh -p 2222 mysql1
root@mysql1's password: 
Last login: Sat Mar 14 22:05:38 2020 from 192.168.199.1
[root@mysql2 ~]# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mTKNynBa-1584197270721)(C:\Users\Administrator\Desktop\Ansible\图片\ansible6.png)]

[root@web1 ~]# ansible ui -m ping
[root@web1 ~]# ansible ui -m ping -k
[root@web1 ~]# ll /root/.ansible/cp/
[root@web1 ~]# ansible ui -m ping

在这里插入图片描述

[root@web1 ~]# vim /etc/ansible/hosts
.. .. .. .. .. ..
[mysql:vars]
ansible_ssh_port=2222

在这里插入图片描述

[root@web1 ~]# ansible mysql -m ping -k

在这里插入图片描述

[root@web1 ~]# vim /etc/ansible/hosts
.. .. .. .. .. .. .. ..
## db-[99:101]-node.example.com

[web]
web1
web2

[mysql]
mysql1 ansible_ssh_port=2222
mysql2

[ui]
xl
[root@web1 ~]# ansible web -m ping -k
[root@web1 ~]# ansible mysql -m ping -k

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g4B8YnjQ-1584197270723)(C:\Users\Administrator\Desktop\Ansible\图片\ansible7.png)]

[root@web1 ~]# vim /etc/ansible/hosts
[web]
web1
web2

[mysql]
mysql1 ansible_ssh_port=2222
mysql2 ansible_ssh_port=2222

[ui]
xl
[root@web1 ~]# ansible mysql -m ping -k

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j253vBWz-1584197270725)(C:\Users\Administrator\Desktop\Ansible\图片\ansible8.png)]

[root@web1 ~]# vim /etc/ansible/hosts
.. .. .. .. .. 
[u:children]
web
mysql

在这里插入图片描述

[root@web1 ~]# ansible web --list-hosts
[root@web1 ~]# ansible mysql --list-hosts
[root@web1 ~]# ansible u --list-hosts

在这里插入图片描述

  • 自定义配置文件
    创建文件夹oxx
    创建配置文件ansible.cfg
[root@web1 ~]# mkdir ooxx
[root@web1 ~]# touch ansible.cfg
[root@web1 ~]# grep -Pv "^(#|$)" /etc/ansible/ansible.cfg

在这里插入图片描述
配置主机文件

[root@web1 ~]# vim /root/ooxx/ansible.cfg
[defaults]
inventory      = /root/ooxx/hosts
host_key_checking = False
[root@web1 ~]# vim /root/ooxx/hosts
[u1]
web1
mysql1

[u2]
web2
mysql2
xl
[root@web1 ~]# cd ooxx
[root@web1 ooxx]# ansible u1 --list-hosts
[root@web1 ooxx]# ansible u2 --list-hosts

在这里插入图片描述

ansible_ssh_host
将要连接的远程主机名与你想要设定的主机别名不同,可以通过此变量设置

ansible_ssh_port
ssh端口号:如果不是默认的端7号,通过此变量设置

ansible_ssh_user
默认的ssh用户名

主机定义与分组( 续3 )

  • inventory参数说明

ansible_ssh_pass
ssh密码(这种方式并不安全我们强烈建议使用-- ask-pass或SSH密钥)

ansible_ sudo_ pass
sudo密码( 建议使用 --ask-sudo-pass )

ansible_sudo_exe ( new in version 1.8 )
sudo命令路径(适用于1.8及以上版本)

主机定义与分组( 续4 )

  • inventory参数说明
    ansible_connection
    与主机的连接类型,如: local ,ssh或paramiko ,1.2以前默认使用paramiko , 1.2以后默认使用’smart’ , 它会根据是否支持ControlPersist来判断’ssh’方式是否可行

ansible_ssh_private_key_file
ssh使用的私钥文件,适用于有多个密钥,而你不想使用SSH代理的情况

主机定义与分组( 续5 )

  • inventory参数说明

ansible_ shell_ type
目标系统的shell类型,默认情况下,命令的执行使用’sh’语法,可设置为’csh’或’fish’

ansible_ python interpreter
目标主机的python路径,适用情况:系统中有多个Python,或者命令路径不是/usr/bin/python’

动态主机

  • 无限可能
    ansible Inventory包含静态和动态的Inventory ,静态Inventory指在文件/etc/ansible/hosts中指定的主机和组动态Inventory指通过外部脚本获取主机列表,按照其要求格式返回给ansilbe命令
  • Json
    JSON ( JavaScript Object Notation,JavaScript对象表示法) ,一种基于文本独立于语言的轻量级数据交换格式

动态主机( 续1 )

  • 注意事项:
    主机部分必须是列表格式
    Hostdata行,其中的"hosts" 部分可以省略,但使用时,必须是"hosts"

动态主机( 续2 )

  • 脚本输出主机列表
[root@web1 ooxx]# vim hosts.py
#!/usr/bin/python
import json

hostlist = {}
hostlist["web"] = ["web1", "web2", "xl"]
hostlist["mysql"] = ["mysql1", "mysql2"]
print json.dumps(hostlist)
[root@web1 ooxx]# chmod 644 hosts
[root@web1 ooxx]# chmod 755 hosts.py
[root@web1 ooxx]# ./hosts.py 
{"web": ["web1", "web2", "xl"], "mysql": ["mysql1", "mysql2"]}
[root@web1 ooxx]# ansible web --list-hosts
[root@web1 ooxx]# vim ansible.cfg
[defaults]
inventory      = /root/ooxx/hosts.py
host_key_checking = False
[root@web1 ooxx]# ansible web --list-hosts
[root@web1 ooxx]# ansible mysql --list-hosts
[root@web1 ooxx]# ansible u1 --list-hosts

在这里插入图片描述

动态主机( 续3 )

  • 脚本输出样例
	{
        "aa" :{
            "hosts" :["192.168.199.147", "192.168.199.148"]
            "vars" :{
                "ansible_ssh_user": "root"
                "ansible_ssh_pass": "pwd"
            }
        },
        "bb" :["192.168.199.156", "192.168.199.154"]
        "192.168.199.158": {"ansible_ssh_user":"root",
                           "ansible_ssh_pass":"pwd"}
    }

批量执行

ansible命令基础

  • ansible < host pattern> [options]
host-pattern主机或定义的分组
- M指定模块路径
- m使用模块,默认command模块
- aor --args模块参数
- iinventory文件路径,或可执行脚本
- k使用交互式登录密码
- e定义变量
- v详细信息,-vvvv开启debug模式

ansible命令基础( 续1 )

  • 格式
[root@web1 ~]# ansible 主机的集合 -m 模块的名称 -a 模块的参数
  • 列出要执行的主机
[root@web1 ~]# ansible all --list-hosts
  • 批量检测主机
[root@web1 ~]# ansible all -m ping
  • 批量执行命令
[root@web1 ~]# ansible all -m command -a 'id' -k
[root@web1 ~]# ansible web -m command -a 'uptime' -k
[root@web1 ~]# ansible mysql -m command -a 'df -h' -k
[root@web1 ~]# ansible all -m command -a 'free -m' -k

在这里插入图片描述### 批量部署证书文件

  • 每次交互输入密码太麻烦
  • 密码写入配置文件安全性很差
  • 不同主机不同密码
  • 使用key方式认证
  • 给所有主机部署公钥
[root@web1 ~]# cd /root/.ssh/
[root@web1 .ssh]# ssh-keygen -t rsa -b 2048 -N ''

在这里插入图片描述

[root@web1 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@web1 ~]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k

在这里插入图片描述

[root@web1 ~]# ssh web2
[root@web1 ~]# ssh -p 2222 mysql1
[root@web1 ~]# ssh -p 2222 mysql2
[root@web1 ~]# ssh xl

在这里插入图片描述

[root@web1 ~]# ansible all -m ping

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值