ansible的使用:
http://perin.blog.51cto.com/10410663/1726277
http://www.mamicode.com/info-detail-1428476.html
一、安裝ansible
1、 python版本需要2.6以上,不過通過centos7都會預設安裝上python2.7.5,檢視方法:python -V
2、 添加yum 源
a、 vim /etc/yum.repos.d/ansible.repo
b、 添加如下内容:
<code>[epel]</code>
<code>name = all </code><code>source</code> <code>for</code> <code>ansible</code>
<code>baseurl = https:</code><code>//mirrors</code><code>.aliyun.com</code><code>/epel/7/x86_64/</code>
<code>enabled = 1</code>
<code>gpgcheck = 0</code>
<code>[ansible]</code>
<code>baseurl = http:</code><code>//mirrors</code><code>.aliyun.com</code><code>/centos/7</code><code>.3.1611</code><code>/os/x86_64/</code>
3、 yum clean all && yum makecache
4、 安裝ansible:yum install ansible -y
二、配置ansible
<code>[root@jeff ansible]</code><code># cat /etc/hosts</code>
<code>172.28.8.61 app_xor1</code>
<code>172.28.8.62 app_xor2</code>
<code>172.28.8.80 gateway</code>
1、 使用ansible前需實作ansible伺服器對另外兩台機的ssh無密碼通路,操作如下:
a、 輸入如下指令,按回車:
<code>ssh</code><code>-keygen -t rsa -P </code><code>''</code>
b、 指令:ssh-copy-id [email protected] ,然後輸入連接配接密碼即可實作無密碼通路。
2、 添加ansible客戶機組,指令:vim /etc/ansible/hosts,在最後面添加如下内容:
<code>[xor]</code>
<code>app_xor1</code>
<code>app_xor2</code>
<code>[docker]</code>
<code>gateway ansible_ssh_user=root ansible_ssh_pass='itvitv'</code>
<code>172.28.8.81 ansible_ssh_user=root ansible_ssh_pass='itvitv'</code>
三、使用ansible
1、 在另外兩台機上執行ping指令:ansible xor -m ping
2、 複制本機檔案到組group1:ansible xor -m copy -a “src=/etc/hosts dest=/etc/”
3、 幫group1安裝軟體:ansible xor -m shell -a “yum install wget -y”
四、FAQ
1、安裝完成後連接配接用戶端伺服器報錯:
<code>[root@jeff ansible]# ansible docker -m ping</code>
<code>172.28.8.81 | FAILED! => {</code>
<code> </code><code>"failed": true, </code>
<code> </code><code>"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."</code>
<code>}</code>
解決:在ansible 伺服器上使用ssh 登陸下/etc/ansible/hosts 裡面配置的伺服器(ssh [email protected])。然後再次使用ansible 去管理就不會報上面的錯誤了!但這樣大批量登陸就麻煩來。因為預設ansible是使用key驗證的,如果使用密碼登陸的伺服器,使用ansible的話,要不修改ansible.cfg配置檔案的ask_pass = True給取消注釋,要不就在運作指令時候加上-k,這個意思是-k, --ask-pass ask for SSH password。再修改:host_key_checking= False即可
本文轉自 326647452 51CTO部落格,原文連結:http://blog.51cto.com/svsky/1964367,如需轉載請自行聯系原作者