一看必會系列:ansible 本站實驗基礎

來源:本站原創 Linux 超過699 views圍觀 0條評論

 

以下為ansible實驗的基礎配置

           

創建免密登陸
[[email protected]_master ~]# ssh-keygen -t rsa -P ”
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
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:
3b:13:81:60:1d:34:54:32:27:b8:35:64:e3:d1:07:8e [email protected]_master
The key’s randomart image is:

 

將.pub導入到目標服務器

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

修改 /etc/ansible/hosts

#以下[ ]中為rols調用名

[test]
192.168.142.104

 

運行 ansible-playbook

ansible-playbook /etc/ansible/roles/xx.yaml -vv

—-技巧————————————–
ansible 常用模塊之command/shell/raw

是否支持管道

module    是否支持管道
command    不支持管道
shell    支持管道——–
raw    支持管道  ——–

使用方法

[[email protected] ~]# ansible localhost -m command -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

[[email protected] ~]# ansible localhost -m shell -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

[[email protected] ~]# ansible localhost -m raw -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello jeff

直接執行某個文件

[[email protected] ~]# ansible host32 -m command -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

[[email protected] ~]# ansible host32 -m raw -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

[[email protected] ~]# ansible host32 -m shell -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello jeff

 

[[email protected]_master roles]# ansible all -m shell -a ‘systemctl status iptables  |grep Active’
192.168.142.102 | SUCCESS | rc=0 >>
   Active: active (exited) since Wed 2018-02-28 11:29:23 CST; 33min ago

192.168.142.104 | SUCCESS | rc=0 >>
   Active: active (exited) since Mon 2018-02-26 17:48:34 CST; 1 day 18h ago

192.168.142.103 | SUCCESS | rc=0 >>
   Active: active (exited) since Mon 2018-02-26 17:49:06 CST; 1 day 18h ago

[[email protected]_master roles]#

ansible-doc -l  列出所有支持的模塊
            -s  列出模塊支持的動作
            -h
            -v 或-vvv 顯示詳細信息

文章出自:CCIE那點事 http://www.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=3742轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
?
?
萌宠夺宝游戏