ansible 學習筆記


安裝

安裝epel源:

rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
yum install -y ansible

ansible命令參數

  -a 'Arguments', --args='Arguments' 命令行參數
  -m NAME, --module-name=NAME 執行模塊的名字,默認使用 command 模塊,所以如果是只執行單一命令可以不用 -m參數
  -i PATH, --inventory=PATH 指定庫存主機文件的路徑,默認為/etc/ansible/hosts.
  -u Username, --user=Username 執行用戶,使用這個遠程用戶名而不是當前用戶
  -U --sud-user=SUDO_User  sudo到哪個用戶,默認為 root
  -k --ask-pass  登錄密碼,提示輸入SSH密碼而不是假設基於密鑰的驗證
  -K --ask-sudo-pass 提示密碼使用sudo
  -s --sudo sudo運行
  -S --su 用 su 命令
  -l  --list 顯示所支持的所有模塊
  -s --snippet 指定模塊顯示劇本片段
  -f  --forks=NUM 並行任務數。NUM被指定為一個整數,默認是5。 #ansible testhosts -a "/sbin/reboot" -f 10 重啟testhosts組的所有機器,每次重啟10台
  --private-key=PRIVATE_KEY_FILE 私鑰路徑,使用這個文件來驗證連接
  -v --verbose 詳細信息
  all  針對hosts 定義的所有主機執行
  -M MODULE_PATH, --module-path=MODULE_PATH 要執行的模塊的路徑,默認為/usr/share/ansible/
  --list-hosts 只打印有哪些主機會執行這個 playbook 文件,不是實際執行該 playbook 文件
  -o --one-line 壓縮輸出,摘要輸出.嘗試一切都在一行上輸出。
  -t Directory, --tree=Directory 將內容保存在該輸出目錄,結果保存在一個文件中在每台主機上。
  -B 后台運行超時時間
  -P 調查后台程序時間
  -T Seconds, --timeout=Seconds 時間,單位秒s
  -P NUM, --poll=NUM 調查背景工作每隔數秒。需要- b
  -c Connection, --connection=Connection  連接類型使用。可能的選項是paramiko(SSH),SSH和地方。當地主要是用於crontab或啟動。
  --tags=TAGS 只執行指定標簽的任務    例子:ansible-playbook test.yml --tags=copy  只執行標簽為copy的那個任務
  --list-hosts 只打印有哪些主機會執行這個 playbook 文件,不是實際執行該 playbook 文件
  --list-tasks 列出所有將被執行的任務
  -C, --check 只是測試一下會改變什么內容,不會真正去執行;相反,試圖預測一些可能發生的變化
  --syntax-check 執行語法檢查的劇本,但不執行它
  -l SUBSET, --limit=SUBSET 進一步限制所選主機/組模式  --limit=192.168.0.15 只對這個ip執行
  --skip-tags=SKIP_TAGS 只運行戲劇和任務不匹配這些值的標簽  --skip-tags=copy_start
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS  額外的變量設置為鍵=值或YAML / JSON
        #cat update.yml
        ---
        - hosts: {{ hosts }}
          remote_user: {{ user }}
        ..............
        #ansible-playbook update.yml --extra-vars "hosts=vipers user=admin"   傳遞{{hosts}}、{{user}}變量,hosts可以是 ip或組名
  -l,--limit 對指定的 主機/組 執行任務  --limit=192.168.0.10,192.168.0.11 或 -l 192.168.0.10,192.168.0.11 只對這個2個ip執行任務

例如:

[root@localhost ansible]# ansible php -m ping -k
SSH password: 
192.168.0.51 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@localhost ansible]# ansible php -a 'pwd' -k
SSH password: 
192.168.0.51 | SUCCESS | rc=0 >>
/root

ansible-doc命令

參數詳解:

[root@localhost ~]# ansible-doc  -h
Usage: ansible-doc [options] [module...]

Show Ansible module documentation     顯示Ansible模塊文檔

Options:
  --version             show program's version number and exit    顯示ansible-doc的版本號
  -h, --help            show this help message and exit  顯示命令參數API文檔
  -M MODULE_PATH, --module-path=MODULE_PATH   查詢模塊,--module-path=MODULE_PATH  指定模塊的路徑
                        Ansible modules/ directory
  -l, --list            List available modules   顯示已存在的所有模塊列表
  -s, --snippet         Show playbook snippet for specified module(s)   顯示playbook制定模塊的用法
  -v                    Show version number and exit   顯示ansible-doc的版本號

例如:

[root@localhost ansible]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success.
  action: ping

ansible-galaxy

參數詳解:

[root@localhost ~]# ansible-galaxy -h
Usage: ansible-galaxy [init|info|install|list|remove] [--help] [options] ...

Options:
  -h, --help  show this help message and exit

See 'ansible-galaxy <command> --help' for more information on a specific command.

ansible-galaxy 指令用於方便的從https://galaxy.ansible.com/ 站點下載第三方擴展模塊,我們可以形象的理解其類似於centos下的yum、python下的pipeasy_install.

例子:

[root@localhost ~]# ansible-galaxy install aeriscloud.docker

- downloading role 'docker', owned by aeriscloud

- downloading role from https://github.com/AerisCloud/ansible-docker/archive/v1.0.0.tar.gz

- extracting aeriscloud.docker to /etc/ansible/roles/aeriscloud.docker

- aeriscloud.docker was installed successfully

ansible-playbook

參數:

[root@localhost ~]# ansible-playbook -h
Usage: ansible-playbook playbook.yml

Options:
  -k, --ask-pass        ask for SSH password
  --ask-su-pass         ask for su password
  -K, --ask-sudo-pass   ask for sudo password
  --ask-vault-pass      ask for vault password
  -C, --check           don't make any changes; instead, try to predict some
                        of the changes that may occur
  -c CONNECTION, --connection=CONNECTION
                        connection type to use (default=smart)
  -D, --diff            when changing (small) files and templates, show the
                        differences in those files; works great with --check
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON
  --force-handlers      run handlers even if a task fails
  -f FORKS, --forks=FORKS
                        specify number of parallel processes to use
                        (default=5)
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY
                        specify inventory host file
                        (default=/etc/ansible/hosts)
  -l SUBSET, --limit=SUBSET
                        further limit selected hosts to an additional pattern
  --list-hosts          outputs a list of matching hosts; does not execute
                        anything else
  --list-tasks          list all tasks that would be executed
  -M MODULE_PATH, --module-path=MODULE_PATH
                        specify path(s) to module library
                        (default=/usr/share/ansible/)
  --private-key=PRIVATE_KEY_FILE
                        use this file to authenticate the connection
  --skip-tags=SKIP_TAGS
                        only run plays and tasks whose tags do not match these
                        values
  --start-at-task=START_AT
                        start the playbook at the task matching this name
  --step                one-step-at-a-time: confirm each task before running
  -S, --su              run operations with su
  -R SU_USER, --su-user=SU_USER
                        run operations with su as this user (default=root)
  -s, --sudo            run operations with sudo (nopasswd)
  -U SUDO_USER, --sudo-user=SUDO_USER
                        desired sudo user (default=root)
  --syntax-check        perform a syntax check on the playbook, but do not
                        execute it
  -t TAGS, --tags=TAGS  only run plays and tasks tagged with these values
  -T TIMEOUT, --timeout=TIMEOUT
                        override the SSH timeout in seconds (default=10)
  -u REMOTE_USER, --user=REMOTE_USER
                        connect as this user (default=root)
  --vault-password-file=VAULT_PASSWORD_FILE
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)
  --version             show program's version number and exit

該指令是使用最多的指令,其通過讀取playbook 文件后,執行相應的動作

ansible-pull

參數:

[root@localhost ~]# ansible-pull -h
Usage: ansible-pull [options] [playbook.yml]

Options:
  -K, --ask-sudo-pass   ask for sudo password    
  -C CHECKOUT, --checkout=CHECKOUT
                        branch/tag/commit to checkout.  Defaults to behavior
                        of repository module.
  -d DEST, --directory=DEST    
                        directory to checkout repository to
  -e EXTRA_VARS, --extra-vars=EXTRA_VARS
                        set additional variables as key=value or YAML/JSON
  -f, --force           run the playbook even if the repository could not be
                        updated
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory-file=INVENTORY    指定庫存主機文件的路徑,默認為/etc/ansible/hosts
                        location of the inventory host file
  -m MODULE_NAME, --module-name=MODULE_NAME   從倉庫檢查的模塊名,默認是git
                        Module name used to check out repository.  Default is
                        git.
  -o, --only-if-changed   
                        only run the playbook if the repository has been
                        updated
  --purge               purge checkout after playbook run   
  -s SLEEP, --sleep=SLEEP
                        sleep for random interval (between 0 and n number of
                        seconds) before starting. this is a useful way to
                        disperse git requests
  -U URL, --url=URL     URL of the playbook repository
  --vault-password-file=VAULT_PASSWORD_FILE
                        vault password file
  -v, --verbose         Pass -vvvv to ansible-playbook

該指令使用需要談到ansible的另一種模式---pull 模式,這和我們平常經常用的push模式剛好相反,其適用於以下場景:你有數量巨大的機器需要配置,即使使用非常高的線程還是要花費很多時間;你要在一個沒有網絡連接的機器上運行Anisble,比如在啟動之后安裝。

ansible-vault

參數:

[root@localhost ~]# ansible-vault 
Usage: ansible-vault [create|decrypt|edit|encrypt|rekey] [--help] [options] file_name

Options:
  -h, --help  show this help message and exit

See 'ansible-vault <command> --help' for more information on a specific command.

ansible-vault主要應用於配置文件中含有敏感信息,又不希望他能被人看到,vault可以幫你加密/解密這個配置文件,屬高級用法。主要對於playbooks里比如涉及到配置密碼或其他變量時,可以通過該指令加密,這樣我們通過cat看到的會是一個密碼串類的文件,編輯的時候需要輸入事先設定的密碼才能打開。這種playbook文件在執行時,需要加上 –ask-vault-pass參數,同樣需要輸入密碼后才能正常執行。

ansible-lint

ansible-lint是對playbook的語法進行檢查的一個工具。用法是ansible-lint playbook.yml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM