mouse 发表于 2018-7-29 09:13:15

ansible 学习记录 1-Jason的博客

  多看书总是让人进步,今天啃了一会儿《奔跑吧Ansible》,重点看了第15页的内容,虽然刚开始的觉得一点没看明白,但是通过search 关键字,渐渐地理解了一点内容,如书中讲的 ansible testserver -i hosts -m ping
  -i 是ansible命令的一个选项,该选项的参数为inventory file,即Inventory配置文件,
  inventory 文件的类似下面的内容:
  testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \
  ansible_ssh_user=root \
  ansible_ssh_private_key_file=
  通过百度搜索,得出了其实可以通过private key 代替ssh 互信来实现anible 操控其他主机。
  最近在作自动化部署发布.读了一下ansible的代码和工作流.
  需要部署一些免密码登录的操作. 记录下
  现在有两台机器, 一台服务器A安装了ansible, 一台是服务器B需要被操作的.
  随便建立一个项目文件夹.
  为了方便管理, 我这样划分的项目
  在conf里面放所有的配置, 包括hosts和ansible.cfg, 然后作一个软链接到最外面. ansible.cfg的优先级将是当前目录最高
  ahttp://img.blog.csdn.net/20151024061037508
  在ssh_keys里面, 存放着很多服务器的私钥. 注意是私钥.
  ansible用了paramiko库.
  ansible.cfg 配置如下. 注意ssh_connection下面的contrl_path=./ssh_keys, 不指定的话会一直报group-readable or world-readable and thus insecure
inventory       = ./conf/hostspull_interval   = 15sudo_user       = roottransport       = paramikomodule_lang   = Chost_key_checking = Falsesudo_exe      = sudo# SSH timeouttimeout         = 30#remote_user   = root#remote_port   = 22remote_tmp      = $HOME/.ansible/tmp  

  
# if True, make ansible use scp if the connection type is ssh, default is sftpscp_if_ssh      = True#sftp_batch_mode= Falsecontrol_path = ./ssh_keys12345678910111213141516171819
  去服务器B上生成密钥对,
#ssh root@服务器B#ssh-keygen 如果要重命名可以自己指定, 回车后生成密钥对# echo ~/.ssh/id_rsa.pub >> known_hosts这里把生成的公钥放入known_hosts如果自己配置了ssh_config, 关闭了known_hosts, 可能就需要写进~/.authorized_keys里面去了.1234  把服务器B上的私钥回传到ansible执行的服务器A的ssh_keys里, 命名为
  服务器B_ip_.key
  给ssh_keys文件夹授权为700, 一定要700, 其它的都会报错.
  建立hosts文件里面指定server, 每个server一行.这里我测试就写一行.
  
  10.0.1.5 ansible_ssh_private_key_file=ssh_keys/10.0.1.5.key ansible_ssh_user=root
  现在回到服务器A的这个ansible文件夹下测试:
  ansible test_server -m shell -a ‘ls -la /etc/hostname’
10.0.1.5 | success | rc=0 >>-rw-r--r-- 1 root root 15 Jun42012 /etc/hostname  其实也可以配置ansible的配置文件:
  
  hostfile = hosts # 注意就是上面提到的文件 位于 /playbook/hosts
  remote_user = vagrant
  private_key_file = .vagrant/machines/defaults/virtualbox/private_key
  host_key_checking = False
  这样的话 执行ansible命令是可以简化为
  ansible testserver -m command -a uptime
  还可以简化(command 模块常用,ansible命令将它设为了默认模块)
  ansible testserver -a uptime
页: [1]
查看完整版本: ansible 学习记录 1-Jason的博客