ansible的参数与基本使⽤⽅法(⼆)
⼀、ansible的配置⽂件介绍
版本:ansible 2.4.2.0
1、配置⽂件
默认路径/etc/ansible/ansible.cfg,这些参数影响到我们后⾯的使⽤,我们这⾥简单看⼏个⽐较常⽤的参数。
#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可以被使⽤.很多⽤户把这个
设置为#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尝试连接超时时间
2、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\)。需要注意的是如果系统⽤的是zsh,有这些类似于*,!,[等这些特殊符号需要⽤单引号包裹。
#module_name: 要执⾏的模块名称,当然 -m 模块名称这⾥在语句中是可选的,默认是command,如ansible all -a "/bin/echo hello"
#arguments:要执⾏的命令语句
#其他主要参数:
-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.1、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.2、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:::
4、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
define的基本用法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⽤户,其他⽤户不设置任何权限。⼀旦你的服务器清单落在⿊客的⼿⾥,那记得机器就直接相当于在互联⽹上裸奔。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论