天天看點

ansible操作

一·、批量推送公鑰

1.使用 ssh-keygen -t rsa生成密鑰對

ssh-keygen -t rsa

2.推送單個公鑰到遠端機器

格式: ssh-copy-id -i ~/.ssh/id_rsa.pub username@[ip,hostname]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

3.添加ansible hosts

編輯/etc/ansible/hosts,沒有則建立些檔案。

格式:【主機名】 【主機位址】 【主機密碼】 預設是root使用者來進行的

[all]

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.批量推送公鑰到遠端機器

[root@mha ansible]# ls

ansible.cfg hosts push.ssh.ymal roles

[root@mha ansible]# 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

ansible操作

5.執行推送指令

ansible-playbook push.ssh.ymal 

[root@mha ansible]# ansible-playbook push.ssh.ymal

PLAY [all] **

TASK [Gathering Facts] **

ok: [192.168.198.134]

ok: [192.168.198.141]

ok: [192.168.198.131]

TASK [ssh-copy] *****

changed: [192.168.198.131]

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操作

二·、檢查主機組或者是單一個主機

[root@mha ansible-tower-setup-3.1.0]# 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 >>

ansible操作

hosts: 192.168.198.131

gather_facts: no

name: mkdir code.

shell: mkdir /root/600

name: git pull the code.

git: [email protected]:wsw26/600.git dest=/root/600/

ansible操作

git: 

repo: [email protected]:wsw26/600.git 

dest: /root/600/ 

version: v1.9.306

ansible操作

客戶機能順利把代碼拉下來并切換分支了

ansible操作

四、ansible-tower

ansible操作
ansible操作

     本文轉自wsw26 51CTO部落格,原文連結:http://blog.51cto.com/wsw26/2062374,如需轉載請自行聯系原作者