ansible基本使用

1. ansible的安裝shell

  1. ansible的特性和優勢
 
1.1 特性
  • no agent:不須要在被管控主機上安裝客戶端,ansible的客戶端就是sshd服務
  • on server:無需服務端,使用時直接在命令行執行命令
  • modules in any languages:基於模塊工做,模塊可以使用任意語言開發
  • yaml, not code:使用yaml格式定製playbook
  • ssh by default:基於ssh協議工做
  • strong mulit-tier soloution:可實現多級指揮
 
1.2 優勢
  • 輕量級,無需安裝agent
  • 批量任務執行能夠寫成腳本,並且不用分發到遠程就能夠執行
  • 支持sudo

 

2. 安裝vim

[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y ansible

 

3. ansible的配置文件ssh

ansible的安裝目錄是/etc/ansible優化

[root@localhost ansible]# ll
總用量 28
-rw-r--r--. 1 root root 19236 2月  10 21:25 ansible.cfg             //配置文件
-rw-r--r--. 1 root root  1031 2月  10 21:19 hosts                   //inventory,ansible須要鏈接的主機列表,能夠填ip或者域名
drwxr-xr-x. 2 root root  4096 1月  30 04:15 roles

 

4. ansible的鏈接優化this

  • 4.1 開啓ssh長鏈接
ansible使用ssh協議和被管控主機通訊,開啓長鏈接後會有一個established的鏈接
openssh5.6之後的版本支持了multiplexing,若是管控機命令行執行ssh -V獲得的版本號大於5.6就能夠設置長鏈接
[root@localhost ansible]# vim ansible.cfg
[ssh_connection]
echo "ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d" >> /etc/ansible/ansible.cfg

ControlPersist=5d 這個參數是設置保持長鏈接的時間spa

 

4.2 取消ssh第一次登錄的交互命令行

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# cat config
UserKnownHostsFile /dev/null
ConnectTimeout 15
StrictHostKeyChecking no

若是是命令行登錄,取消第一次交互:ssh -o StrictHostKeyChecking=no 192.168.123.107code

 

4.3 去除ansible第一次執行命令而沒有known_hosts文件報錯限制server

第一次執行ansible命令,可是管控機歷來沒有登陸過被管控機,會報以下錯誤blog

[root@localhost ~]# ansible all -i test.txt -m ping -k
SSH password:
192.168.123.107 | FAILED! => {
    "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."
}

 

解決方法:

[root@localhost ~]# vim /etc/ansible/ansible.cfg
.......
# uncomment this to disable SSH key host checking
host_key_checking = False  

 

 

ansible的模塊

大部分模塊之前都學過,再也不重複作筆記,只補充之前沒學過的
 
ansible執行命令的格式:ansible ip/group -m module -a arg
 
1. ping模塊
[root@localhost ansible]# ansible all -m ping
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

2. 使用指定的IP列表文件

[root@localhost ~]# cat test.txt
192.168.123.107

[root@localhost ~]# ansible all -i test.txt -m ping      //若是自定義的文件沒有分組,調用文件時必須寫上all              
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

3. ansible使用密碼登錄被管控主機

ansible執行命令前,管控機須要配置密鑰登錄被管控機,使用密碼則省去了這一步
有兩種方式實現密碼登錄,第一種是ansible -k 選項
[root@localhost ~]# ansible -h |grep ask-pass
    -k, --ask-pass      ask for connection password

[root@localhost ~]# ansible all -i test.txt -m ping -k
SSH password:
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

第二種是在inventory裏,ip 後面直接把密碼寫上

[root@localhost ~]# cat test.txt
192.168.123.107 ansible_ssh_pass=123456                         //直接在ip後面寫上密碼

[root@localhost ~]# ansible all -i test.txt -m ping
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

4. 收集被管控機的信息(相似saltstack的grains)

[root@localhost ~]# ansible all -i test.txt -m setup             //調用setup模塊

[root@localhost ~]# ansible all -i test.txt -m setup -a 'filter=ansible_default_ipv4'             //使用filter關鍵字對setup的信息過濾提取
192.168.123.107 | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.123.107",
            "alias": "ens33",
            "broadcast": "192.168.123.255",
            "gateway": "192.168.123.1",
            "interface": "ens33",
            "macaddress": "00:0c:29:66:54:78",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.123.0",
            "type": "ether"
        }
    },
    "changed": false
}

 

5. 從inventory中取ip地址

[root@localhost ~]# ansible all -i test.txt -m shell -a 'echo {{ inventory_hostname}}'       //inventory_hostname是一個內置變量,返回當前的inventory
192.168.123.107 | SUCCESS | rc=0 >>
192.168.123.107
相關文章
相關標籤/搜索