版本:ansible 2.4.2.0html
一、配置文件web
默认路径/etc/ansible/ansible.cfg,这些参数影响到咱们后面的使用,咱们这里简单看几个比较经常使用的参数。api
1.一、这里能够参考http://www.51niux.com/?id=47这篇博客,楼主写的仍是很清楚。 缓存
1.二、官方文档https://docs.ansible.com/ansible/latest/index.html安全
#inventory = /etc/ansible/hosts #被控制的主机配置文件 #library = /usr/share/my_modules/ ##Ansible默认搜寻模块的位置 #remote_tmp = ~/.ansible/tmp #远程主机缓存目录 #local_tmp = ~/.ansible/tmp #本地缓存目录 #forks = 5 ##这个选项设置在与主机通讯时的默认并行进程数.从Ansible 1.3开始,fork数量默认自动设置为主机数量或者潜在的主机数量, 这将直接控制有多少网络资源活着cpu能够被使用.不少用户把这个设置为50,有些设置为500或者更多.若是你有不少的主机, 高数值将会使得跨主机行为变快.默认值比较保守. #poll_interval = 15 ##对于Ansible中的异步任务, 这个是设置定义,当具体的poll interval 没有定义时,多少时间回查一下这些任务的状态, 默认值是一个折中选择15秒钟.这个时间是个回查频率和任务完成叫回频率和当任务完成时的回转频率的时钟。 #sudo_user = root #默认sudo用户 #ask_sudo_pass = True #默认sudo用户是否须要输入密码 #ask_pass = Fales #每次执行都须要询问ssh密码 #remote_port = 22 #被控主机默认端口 #module_lang = C #ansible默认语言 timeout = 10 #默认ssh尝试链接超时时间
二、ansible的语法格式服务器
标准格式:ansible <pattern_goes_here> -m <module_name> -a <arguments>
#pattern_goes_here: 能够是某一个主机IP(如192.168.1.128)或者是hosts中的组名(web.server),192.168.1.*表明全部可是前提是hosts文件里面已经作好了定义如192.168.1.[1:254]这样就是执行的192.168.1.1-192.168.1.254,同时制定多个组或者多个ip使用;分割,用!表示非(如web.server:!data.server表示在web.server主机组里面可是不在data.server主机组里面的主机IP),用&表示交集部分,能够指定分组的下标或切片(如web.server[1],就是这个主机组第二个主机IP,须要主机的是超过了范围就没法匹配了),能够用~开头来使用正则(如:~(web|data).*\.server\.com)。须要注意的是若是系统用的是zsh,有这些相似于*,!,[等这些特殊符号须要用单引号包裹。网络
#module_name: 要执行的模块名称,固然 -m 模块名称 这里在语句中是可选的,默认是command,如ansible all -a "/bin/echo hello"并发
#arguments:要执行的命令语句ssh
#其余主要参数:异步
-u username #指定ssh链接的用户名,即执行后面命令的用户
-k #若是是普通用户运行ansible,当须要root权限执行的话,-k参数用来输入密码
-i inventory_file #指定所使用的用户组所在的文件,默认是/etc/ansible/hosts
-f number #指定并发数,并发量大的时候,提升该数字值,如 -f 10,默认是5个。
-t #输出结果保存路径
-K #sudo密码(若是执行命令须要使用sudo用户的话,须要-u执行用户,用-K执行输入sudo密码)
--private-key=key_name #若是是用非默认的id_rsa来当私钥的话,能够指定一个私钥文件。
3 ansible的远程控制方式
3.一、ansible之因此不须要安装客户端,是由于它使用ssh,因此这里无非是作ssh信任或者执行输入密码。上篇咱们说了如何ssh信任(免除每次输入密码),下面咱们来讲须要输入密码的方式,默认不输入密码提示拒绝访问。这里只须要加一个-k的参数,就能够了,只要不重启被控端,之心命令之后就不用加-k的参数了。
[root@localhost ansible]# ansible -i /etc/ansible/hosts test1 -a 'whoami' 172.16.5.239 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": true }
3.二、sudo使用方法
这里咱们建了一个test用户,而且设置了密码,模拟没有root权限的状况下,使用sudo权限。这里须要注意的是否是-k,而是-K,命令前加sudo。(特别提示,默认状况下普通用户是没有查看这个文件的权限)
[root@localhost ~]# ansible test1 -a 'sudo cat /etc/shadow' -u test -K SUDO password: [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo 172.16.5.239 | SUCCESS | rc=0 >> root:$6$Dzqyob0uMKLCpruY$APjNbel3k2uJmLXF9v44eh8fHH6To7TMcFP0oVP9ftxpluU/d95SblLDypFaU85VDmL9vR3jj3vyB/gDNlE3f/::0:99999:7::: bin:*:17110:0:99999:7::: daemon:*:17110:0:99999:7::: adm:*:17110:0:99999:7::: lp:*:17110:0:99999:7::: sync:*:17110:0:99999:7::: shutdown:*:17110:0:99999:7::: halt:*:17110:0:99999:7::: mail:*:17110:0:99999:7::: operator:*:17110:0:99999:7::: games:*:17110:0:99999:7::: ftp:*:17110:0:99999:7::: nobody:*:17110:0:99999:7::: systemd-network:!!:17599:::::: dbus:!!:17599:::::: polkitd:!!:17599:::::: postfix:!!:17599:::::: sshd:!!:17599:::::: chrony:!!:17599:::::: test:$6$0ArITjlE$LBTf3a5bfIZcphKIFSMHFnd3e/ubAslekmA23deE/Pyw5fBajOCFui2oy02jLZgB8ZJ38qZh.wAuBUgROyLj00:17786:0:99999:7:::
四、ansible的经常使用命令
ansible --h #--h参数你们都知道了啊,就是查看有哪些参数解释帮助说明
Usage: ansible <host-pattern> [options] Define and run a single task 'playbook' against a set of hosts Options: -a MODULE_ARGS, --args=MODULE_ARGS 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, if filename prepend with @ -f FORKS, --forks=FORKS specify number of parallel processes to use (default=5) -h, --help show this help message and exit -i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY specify inventory host path or comma separated host list. --inventory-file is deprecated -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 prepend colon-separated path(s) to module library (default=[u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']) --new-vault-id=NEW_VAULT_ID the new vault identity to use for rekey --new-vault-password-file=NEW_VAULT_PASSWORD_FILES new vault password file for rekey -o, --one-line condense output -P POLL_INTERVAL, --poll=POLL_INTERVAL 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-id=VAULT_IDS the vault identity to use --vault-password-file=VAULT_PASSWORD_FILES vault password file -v, --verbose verbose mode (-vvv for more, -vvvv to enable connection debugging) --version show program's version number and exit Connection Options: control as whom and how to connect to hosts -k, --ask-pass ask for connection password --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, --connection=CONNECTION connection type to use (default=smart) -T TIMEOUT, --timeout=TIMEOUT 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) -S, --su run operations with su (deprecated, use become) -R SU_USER, --su-user=SU_USER run operations with su as this user (default=None) (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 | pmrun ] --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 ask for privilege escalation password
ansible-doc -h
Options: -a, --all **For internal testing only** Show documentation for all plugins. -h, --help show this help message and exit -l, --list 列出可用的模块 -M MODULE_PATH, --module-path=MODULE_PATH prepend colon-separated path(s) to module library (default=[u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']) -s, --snippet 显示指定模块的剧本片断,-s 模块名称 能够查看某个模块详细的使用方法 -t TYPE, --type=TYPE Choose which plugin type (defaults to "module") -v, --verbose verbose mode (-vvv for more, -vvvv to enable connection debugging) --version show program's version number and exit
注意:这里须要特别注意无论是静态inventory,仍是动态生成的inventory,仍是咱们把重要信息写入到的ansible.cfg,为了安全起见,通常状况下除了root用户,其余用户不设置任何权限。一旦你的服务器清单落在黑客的手里,那记得机器就直接至关于在互联网上裸奔。