基於docker使用ansible測試示例
阿新 • • 發佈:2019-01-14
一、新建4個虛擬主機
3個節點當作伺服器
docker run -d --name node2 -p 2223:22 chenqionghe/ubuntu docker run -d --name node3 -p 2224:22 chenqionghe/ubuntu docker run -d --name node4 -p 2225:22 chenqionghe/ubuntu
一個節點安裝ansible
docker run -d --name node1 -p 2222:22 \ --link node2:node2 \ --link node3:node3 \ --link node4:node4 \ chenqionghe/ubuntu
二、ssh連線node1進行準備操作
密碼lightWeightBaby!
ssh [email protected] -p 2222
安裝ssh-copy-id
curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh chmod 755 /usr/local/bin/ssh-copy-id
生成ssh公鑰
ssh-keygen
安裝ssh客戶端
yum update && yum -y install openssh-clients
設定3個節點免密碼登入
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
安裝ansible
yum install ansible
配置ansible的hosts,編輯/etc/ansible/hosts,加入
[web] node2 node3 node4
三、使用ansible
列出所有命令
ansible-doc -l
列出指定命令使用方法,如ansible-doc -s command
ansible-doc -s 命令名
使用command模組
[[email protected] tmp]# ansible web -m command -a 'date' node3 | SUCCESS | rc=0 >> Mon Jan 14 08:38:57 UTC 2019 node2 | SUCCESS | rc=0 >> Mon Jan 14 08:38:57 UTC 2019 node4 | SUCCESS | rc=0 >> Mon Jan 14 08:38:57 UTC 2019
使用ping模組
[[email protected] tmp]# ansible all -m ping node2 | SUCCESS => { "changed": false, "ping": "pong" } node4 | SUCCESS => { "changed": false, "ping": "pong" } node3 | SUCCESS => { "changed": false, "ping": "pong" }
使用user模組,新增使用者cqh
ansible web -m user -a "name=cqh"
使用shell模組,給cqh設定密碼
ansible web -m shell -a 'echo superman|passwd --stdin cqh'
使用script模組(相對路徑)
cd /tmp echo "echo \"hello cqh\" > /tmp/test.txt" > test.sh ansible web -m script -a "test.sh" #檢視是否生效 ansible web -a "cat /tmp/test.txt"