Usage: ansible <host-pattern> [options] Options: -a MODULE_ARGS, --args=MODULE_ARGS module arguments -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 -B SECONDS, --background=SECONDS run asynchronously, failing after X seconds (default=N/A) -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) -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 -m MODULE_NAME, --module-name=MODULE_NAME module name to execute (default=command) -M MODULE_PATH, --module-path=MODULE_PATH specify path(s) to module library (default=/usr/share/ansible) -o, --one-line condense output -P POLL_INTERVAL, --poll=POLL_INTERVAL set the poll interval if using -B (default=15) --private-key=PRIVATE_KEY_FILE use this file to authenticate the connection -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) -T TIMEOUT, --timeout=TIMEOUT override the SSH timeout in seconds (default=10) -t TREE, --tree=TREE log output to this directory -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
匹配所有主机
# ansible all -m ping -u root
匹配指定IP地址
# ansible 192.168.2.9 -m ping -u root 192.168.2.9 | success >> { "changed": false, "ping": "pong" }
使用通配符
# ansible 192.168.2.? -m ping -u root 192.168.2.9 | success >> { "changed": false, "ping": "pong" } # ansible 192.168.2.* -m ping -u root 192.168.2.12 | success >> { "changed": false, "ping": "pong" } 192.168.2.15 | success >> { "changed": false, "ping": "pong" } 192.168.2.9 | success >> { "changed": false, "ping": "pong" } 192.168.2.11 | success >> { "changed": false, "ping": "pong" }
制定一个hosts文件
# echo '192.168.6.10' >> hosts # cat hosts 192.168.6.10 # ansible all -a 'echo hello' -i hosts -u root 192.168.6.10 | success | rc=0 >> hello
$ ansible all -m ping -u neo
ansible all -m copy -a "src=hosts dest=~/hosts" -i hosts -u vagrant
sudo 模式
# as bruce, sudoing to root $ ansible all -m ping -u neo --sudo # as bruce, sudoing to batman $ ansible all -m ping -u www --sudo --sudo-user neo
$ ansible all -m ping -u www
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。