亚洲熟女综合色一区二区三区,亚洲精品中文字幕无码蜜桃,亚洲va欧美va日韩va成人网,亚洲av无码国产一区二区三区,亚洲精品无码久久久久久久

通過(guò)堡壘機(jī)代理SSH運(yùn)行Ansible(譯)

有一種常見(jiàn)的網(wǎng)絡(luò)安全模式是阻止私有網(wǎng)絡(luò)外部對(duì)應(yīng)用服務(wù)器的所有連接(指除了業(yè)務(wù)數(shù)據(jù)外其它的連接,如后臺(tái)管理系統(tǒng)和內(nèi)部業(yè)務(wù)系統(tǒng)。譯者注),然后使用 DMZ 區(qū)域中的 堡壘機(jī) 來(lái)選擇性的將到服務(wù)器的流量加入白名單。

我們有這樣的一個(gè)服務(wù)器池,只允許來(lái)自特定 IP 地址的 SSH 流量。這些服務(wù)器還由 Ansible 通過(guò) SSH 方式程序化的來(lái)管理。

堡壘機(jī)方式導(dǎo)致 Ansible 不能直接與應(yīng)用服務(wù)器通訊,因此需要找到通過(guò)堡壘機(jī)代理 SSH 連接的方法。

我喜歡用 Ansible 創(chuàng)建簡(jiǎn)單任務(wù)來(lái)運(yùn)行,比如清空 memcache 以消除緩存。運(yùn)行這個(gè)例子,以下是 Ansible 結(jié)構(gòu):

devops/
    ansible/
        roles/
            memcache/
                tasks/
                    main.yml
                    restart.yml
        tasks/
            restart-memcache.yml
        vars/
            production-memcache.yml
    bin/
        restart-production-memcache.sh
    hosts.ini
    ssh.config
    ansible.cfg

腳本 tasks/restart-production-memcache.sh 如下:

#!/bin/sh
ssh-add ${DEPLOY_KEYS_DIR}/memcache-servers.pem

ansible-playbook -i ansible/hosts.ini -u ansible ansible/tasks/restart-memcache.yml -v

從 devops 根目錄執(zhí)行,首先將服務(wù)器的 SSH key 添加到 SSH 客戶端代理,然后執(zhí)行 restart-memcache.ymlplaybook ,這包含了 memcache 角色的 restart.yml playbook (以及執(zhí)行其它任務(wù))。

ssh.config 文件中有以下 SSH 配置:

Host bastion
    User                   ec2-user
    HostName               ###.###.###.###
    ProxyCommand           none
    IdentityFile           /path/to/ssh/key.pem
    BatchMode              yes
    PasswordAuthentication no

Host *
    ServerAliveInterval    60
    TCPKeepAlive           yes
    ProxyCommand           ssh -q -A ec2-user@###.###.###.### nc %h %p
    ControlMaster          auto
    ControlPath            ~/.ssh/mux-%r@%h:%p
    ControlPersist         8h
    User                   ansible
    IdentityFile           /path/to/ssh/key.pem

首先聲明用于連接到堡壘機(jī)的配置。緊接著是一個(gè)包含所有主機(jī)的總配置,在 ProxyCommand 中指明首先連接到堡壘機(jī),然后使用 netcat (`nc) 來(lái)傳遞 Ansible 命令到應(yīng)用服務(wù)器。

在 devops 文件夾中運(yùn)行 ssh bastion -F ssh.config 即可連接到堡壘機(jī)。

接下來(lái) Ansible 在連接應(yīng)用服務(wù)器時(shí),需要被告知使用這個(gè)自定義的 SSH 配置。

ansible.cfg 文件中有如下配置:

[ssh_connection]
ssh_args = -o ControlPersist=15m -F ssh.config -q
scp_if_ssh = True
control_path = ~/.ssh/mux-%%r@%%h:%%p

當(dāng) Ansible 在 devops 中執(zhí)行時(shí),能自動(dòng)選擇 ansible.cfg 并在運(yùn)行 playbooks 時(shí)使用定義的配置項(xiàng)。

這種設(shè)置方法的一個(gè)問(wèn)題是它運(yùn)行時(shí)的 Ansible 的輸出非常冗長(zhǎng),因?yàn)橥ㄟ^(guò)堡壘機(jī)連接到應(yīng)用服務(wù)器時(shí),包含了 SSH 調(diào)試連接信息;暫未找到好的辦法來(lái)跳過(guò)這些信息。


譯者:Liu Lantao : http://blog.liulantao.com

來(lái)源: Using Ansible with a bastion SSH host by Alex Bilbie

相關(guān)新聞

歷經(jīng)多年發(fā)展,已成為國(guó)內(nèi)好評(píng)如潮的Linux云計(jì)算運(yùn)維、SRE、Devops、網(wǎng)絡(luò)安全、云原生、Go、Python開(kāi)發(fā)專業(yè)人才培訓(xùn)機(jī)構(gòu)!