Ansible的安裝

安裝ansible

測試機器環(huán)境

3臺centos7.5

ip1:192.168.12.14
ip2:192.168.12.15
ip3:192.168.12.16

ip1為主服務器安裝控制端

默認yum安裝就好旁瘫,命令如下:
[root@wsn-ansible1 ~]# yum -y install ansible
[root@wsn-ansbile1 ~]# ansible --version
ansible 2.7.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]

Ansible配置及測試

ansible的主機和組的默認配置在/etc/ansible/hosts膝但,我們可以不用默認配置直接建立一個新的配置猜煮,使用的時候指定就可以。

建立一個放ansible配置的目錄和文件

[root@wsn-ansbile1 ~]# mkdir -p /root/ansible_test
[root@wsn-ansbile1 ~]# cd /root/ansible_test/
[root@wsn-ansbile1 ansible_test]# touch host.wsn
[root@wsn-ansbile1 ansible_test]# vim host.wsn
內容如下:
[webservers]
192.168.12.15
192.168.12.16

[root@wsn-ansbile1 ansible_test]# ansible -i host.wsn 192.168.12.15 -m ping -k
SSH password: 
192.168.12.15 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

[root@wsn-ansible1 ansible_test]# ansible -i host.wsn webservers -m ping -k
SSH password: 
192.168.12.15 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.12.16 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

由于主控機器和被控機器沒有建立免密碼登錄所以要加上-k參數(shù)旺聚,提供root的密碼杈绸,-i參數(shù)是指定主機和組的配置辆布,webservers是組名丘侠。

配置Linux免密碼登錄

首先關閉selinux不然會出現(xiàn)問題命令如下:

[root@wsn-ansible1 ~]# setenforce 0
上面的命令是臨時關閉,建議永久關閉
[root@wsn-ansible1 ~]# vim /etc/selinux/config
SELINUX=disabled 改成disabled

創(chuàng)建免密碼密鑰

[root@wsn-ansible1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LQB7G8BSr4KNNbouZ0x0BY8oxXK3FtKk9tr5dsQ7Z7M root@wsn-ansible1
The key's randomart image is:
+---[RSA 2048]----+
| ..=*.           |
|..=o=B.          |
|.o*+o+*          |
| O.o+o + .       |
|+.ooo ..S .      |
| ..+ .  o.       |
|.o. o  . .       |
|o +  .. + +      |
|.+   ... +Eo     |
+----[SHA256]-----+
[root@wsn-ansible1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.14
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.12.14 (192.168.12.14)' can't be established.
ECDSA key fingerprint is SHA256:GDKal+vbb4MrmyKyw3EtZBUAtlRnKI/FAxRE1Z+fa+E.
ECDSA key fingerprint is MD5:6f:af:69:ef:ff:34:a6:89:0e:88:34:21:8a:0a:0f:38.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.12.14's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.12.14'"
and check to make sure that only the key(s) you wanted were added.

[root@wsn-ansible1 ~]# ssh 192.168.12.14
Last login: Tue Nov 20 10:43:14 2018
[root@wsn-ansible1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.15
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.12.15's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.12.15'"
and check to make sure that only the key(s) you wanted were added.

[root@wsn-ansible1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.16
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.12.16's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.12.16'"
and check to make sure that only the key(s) you wanted were added.

[root@wsn-ansible1 ~]# ssh 192.168.12.15
Last failed login: Tue Nov 20 11:01:23 CST 2018 from 192.168.12.14 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Nov 20 10:47:45 2018 from 192.168.12.14
[root@wsn-absible2 ~]# exit
logout
Connection to 192.168.12.15 closed.
[root@wsn-ansible1 ~]# ssh 192.168.12.16
Last login: Tue Nov 20 10:47:45 2018 from 192.168.12.14
[root@wsn-absilbe3 ~]# exit
logout
Connection to 192.168.12.16 closed.
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末攒磨,一起剝皮案震驚了整個濱河市泳桦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌娩缰,老刑警劉巖灸撰,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拼坎,居然都是意外死亡浮毯,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門泰鸡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來债蓝,“玉大人,你說我怎么就攤上這事盛龄∈渭#” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵余舶,是天一觀的道長啊鸭。 經(jīng)常有香客問我,道長匿值,這世上最難降的妖魔是什么赠制? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮挟憔,結果婚禮上钟些,老公的妹妹穿的比我還像新娘。我一直安慰自己绊谭,他們只是感情好厘唾,可當我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著龙誊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪喷楣。 梳的紋絲不亂的頭發(fā)上趟大,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天鹤树,我揣著相機與錄音,去河邊找鬼逊朽。 笑死罕伯,一個胖子當著我的面吹牛,可吹牛的內容都是我干的叽讳。 我是一名探鬼主播追他,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼岛蚤!你這毒婦竟也來了邑狸?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤涤妒,失蹤者是張志新(化名)和其女友劉穎单雾,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體她紫,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡硅堆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了贿讹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渐逃。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖民褂,靈堂內的尸體忽然破棺而出茄菊,到底是詐尸還是另有隱情,我是刑警寧澤助赞,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布买羞,位于F島的核電站,受9級特大地震影響雹食,放射性物質發(fā)生泄漏畜普。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一群叶、第九天 我趴在偏房一處隱蔽的房頂上張望吃挑。 院中可真熱鬧,春花似錦街立、人聲如沸舶衬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逛犹。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間虽画,已是汗流浹背舞蔽。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留码撰,地道東北人渗柿。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像脖岛,于是被迫代替她去往敵國和親朵栖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,066評論 2 355