mancha 发表于 2018-7-29 07:23:06

ansible操作

  一、安装

  修改管理员密码

  二、批量推送公钥
  1.使用 ssh-keygen -t rsa生成密钥对
  ssh-keygen -t rsa
  2.推送单个公钥到远程机器
  格式: ssh-copy-id -i ~/.ssh/id_rsa.pub username@
  ssh-copy-id -i ~/.ssh/id_rsa.pub username@192.168.198.x
  3.添加ansible hosts
  编辑/etc/ansible/hosts,没有则创建些文件。
  格式:【主机名】 【主机地址】 【主机密码】默认是root用户来进行的
  
  1 ansible_ssh_user="root"ansible_ssh_host=192.168.198.131 ansible_ssh_pass="123456"
  2 ansible_ssh_user="root"ansible_ssh_host=192.168.198.134 ansible_ssh_pass="123456"
  3 ansible_ssh_user="root"ansible_ssh_host=192.168.198.141 ansible_ssh_pass="123456"
  新版的ansible(2.4) hosts有更新, 用以下方式:
  
  192.168.198.131 ansible_user=root ansible_ssh_pass="123456"
  192.168.198.141 ansible_user=root ansible_ssh_pass="123456"
  192.168.198.134 ansible_user=root ansible_ssh_pass="123456"
  4.批量推送公钥到远程机器
  机器多的情况下,使用ssh-copy-id方法有些费时,使用ansible-playbook推送ymal,这里使用到了authoried_keys模块,可以参考 http://docs.ansible.com/authorized_key_module.html
  # ls
  ansible.cfghostspush.ssh.ymalroles
  # cat push.ssh.ymal
  

- hosts: all  user: root
  tasks:
  - name: ssh-copy
  authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
  tags:
  - sshkey
  


  5.执行推送命令
  ansible-playbook push.ssh.ymal
  # ansible-playbook push.ssh.ymal
  PLAY **
  TASK **
  ok:
  ok:
  ok:
  TASK *****
  ok:
  ok:
  changed:
  PLAY RECAP **
  192.168.198.131            : ok=2    changed=1    unreachable=0    failed=0
  192.168.198.134            : ok=2    changed=0    unreachable=0    failed=0
  192.168.198.141            : ok=2    changed=0    unreachable=0    failed=0

  新增用户和创建密码
  # ansible nginx -m shell -a "useradd wsw && echo "123456"| passwd --stdin wsw"
  192.168.198.131 | SUCCESS | rc=0 >>
  Changing password for user wsw.
  passwd: all authentication tokens updated successfully.
  192.168.198.134 | SUCCESS | rc=0 >>
  Changing password for user wsw.
  passwd: all authentication tokens updated successfully.
  192.168.198.145 | SUCCESS | rc=0 >>
  Changing password for user wsw.
  passwd: all authentication tokens updated successfully.
  192.168.198.144 | SUCCESS | rc=0 >>
  Changing password for user wsw.
  passwd: all authentication tokens updated successfully.
  三、检查主机组或者是单一个主机
  # ansible all -m shell -a "mysql -uroot -p123456 -e 'use test;show tables'"
  192.168.198.134 | SUCCESS | rc=0 >>
  Tables_in_test
  t3
  t4
  t44
  t5
  tt1
  192.168.198.131 | FAILED | rc=127 >>
  /bin/sh: mysql: command not found
  192.168.198.141 | SUCCESS | rc=0 >>
  Tables_in_test
  t3
  t4
  t44
  t5
  tt1

  四、ansible-playbook
  1、关于git pull
  # cat git.yml
  

---  
- hosts: 192.168.198.131
  gather_facts: no
  

  tasks:
  - name: mkdir code.
  shell: mkdir /root/600
  

  tasks:
  - name: git pull the code.
  git: repo=git@git.coding.net:wsw26/600.gitdest=/root/600/
  


  2、关于git pull紧接着checkout branch
  # cat git.yml
  

---  
- hosts: 192.168.198.131
  gather_facts: no
  

  tasks:
  - name: mkdir code.
  shell: mkdir /root/600
  

  tasks:
  - name: git pull the code.
  git: repo=git@git.coding.net:wsw26/600.gitdest=/root/600/
  

  tasks:
  - git:
  repo: git@git.coding.net:wsw26/600.git
  dest: /root/600/
  version: v1.9.306
  


  客户机能顺利把代码拉下来并切换分支了

  四、ansible-tower


  编译打开
页: [1]
查看完整版本: ansible操作