$ cd /etc/yum.repo.d/
$ wget http://mirrors.aliyun.com/repo/epel-6.repo
#下载源
Markdown
2.配置文件
$ cd /etc/ansible/
ll
total 28
-rw-r--r-- 1 root root 18066 Jun 2 05:49 ansible.cfg #ansible主配置文件
-rw-r--r-- 1 root root 1016 Jun 2 05:49 hosts #定义主机组
drwxr-xr-x 2 root root 4096 Jun 2 05:49 roles #定义规则
Markdown
二、配置ansible
1.为远程连接主机生成公/私钥。
$ ssh-keygen -t rsa -P '' #-t生成密钥类似(rsa/dsa) -P提供旧密码,'' 表示没有
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5b:9e:3b:b7:04:47:e8:b3:28:cd:5a:ff:5e:a3:ae:b2 root@TLCUM01
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
| . . |
| . . |
| S = . |
| o = * |
| . * + . o |
| +...o.o . |
| . Eo+B*. |
+-----------------+
$ ansible
Usage: ansible <host-pattern> [options]
Options:
-a MODULE_ARGS, --args=MODULE_ARGS #模块的参数,如果执行默认COMMAND的模块,即是命令参数,如:“date”,"pwd"等等
module arguments #模块参数
--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 #只是测试一下会改变什么内容,不会真正去执行;相反,试图预测一些可能发生的变化
-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
-f FORKS, --forks=FORKS #并行任务数。NUM被指定为一个整数,默认是5
specify number of parallel processes to use (default=5)
-h, --help show this help message and exit #打开帮助文档API
-i INVENTORY, --inventory-file=INVENTORY #指定库存主机文件的路径,默认为/etc/ansible/hosts
specify inventory host path (default=/etc/ansible/hosts) or comma separated host list.
-l SUBSET, --limit=SUBSET #进一步限制所选主机/组模式 --limit=192.168.91.135 只对这个ip执行
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 #执行模块的名字,默认使用 command 模块,所以如果是只执行单一命令可以不用 -m参数
module name to execute (default=command)
-M MODULE_PATH, --module-path=MODULE_PATH #要执行的模块的路径,默认为/usr/share/ansible/
specify path(s) to module library (default=None)
--new-vault-password-file=NEW_VAULT_PASSWORD_FILE
new vault password file for rekey
-o, --one-line condense output #压缩输出,摘要输出.尝试一切都在一行上输出。
--output=OUTPUT_FILE output file name for encrypt or decrypt; use - for
stdout
-P POLL_INTERVAL, --poll=POLL_INTERVAL #调查背景工作每隔数秒。需要-b
set the poll interval if using -B (default=15)
--syntax-check perform a syntax check on the playbook, but do not
execute it
-t TREE, --tree=TREE log output to this directory #将日志内容保存在该输出目录,结果保存在一个文件中在每台主机上。
--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的版本
Connection Options:
control as whom and how to connect to hosts -k, --ask-pass ask for connection password #登录密码,提示输入SSH密码而不是假设基于密钥的验证 --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE use this file to authenticate the connection -u REMOTE_USER, --user=REMOTE_USER connect as this user (default=None) -c CONNECTION, --con\=CONNECTION #连接类型使用。可能的选项是paramiko(SSH),SSH和地方。当地主要是用于crontab或启动。 connection type to use (default=smart) -T TIMEOUT, --timeout=TIMEOUT #指定SSH默认超时时间, 默认是10S override the connection timeout in seconds (default=10) --ssh-common-args=SSH_COMMON_ARGS specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand) --sftp-extra-args=SFTP_EXTRA_ARGS specify extra arguments to pass to sftp only (e.g. -f, -l) --scp-extra-args=SCP_EXTRA_ARGS specify extra arguments to pass to scp only (e.g. -l) --ssh-extra-args=SSH_EXTRA_ARGS specify extra arguments to pass to ssh only (e.g. -R)
Privilege Escalation Options:
control how and which user you become as on target hosts -s, --sudo run operations with sudo (nopasswd) (deprecated, use become) -U SUDO_USER, --sudo-user=SUDO_USER desired sudo user (default=root) (deprecated, use become) #远程用户, 默认是root用户 -S, --su run operations with su (deprecated, use become) -R SU_USER, --su-user=SU_USER #指定SU的用户,默认是root用户 run operations with su as this user (default=root) (deprecated, use become) -b, --become run operations with become (does not imply password prompting) --become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo | ksu | runas ] --become-user=BECOME_USER run operations as this user (default=root) --ask-sudo-pass ask for sudo password (deprecated, use become) --ask-su-pass ask for su password (deprecated, use become) -K, --ask-become-pass #提示密码使用sudo,sudo表示提权操作 ask for privilege escalation password