Ansible的设计非常简单,他开发的方法就是将我们的代码通过ssh连接来发送和执行,这意味这在你的受管机器上不需要安装Ansible,这也意味这你可以共享你现在用的ssh通道。
首先,让我们用Ansible的ping模块来验证到受管机器的连接状态,执行下面的命令连接到受管机器:
$ ansible site01 -u root -k -m ping
这条命令会要求你输入ssh密码,并返回一个执行结果,如下:
site01 | success >> {
"changed": false,
"ping": "pong"
}
如果你有远程受管机器的ssh密钥,你就可以不使用-k参数,而使用密钥文件来连接远程受管机器。你还可以通过配置清单中主机选项中的参数和全局配置中的参数来设置你每次要连接远程受管机器时使用的用户名。
[webservers]
#1
site01 ansible_ssh_user=root #改变连接远程受管机器时使用的用户名
#2
site02 ansible_ssh_user=daniel #改变连接远程受管机器时使用的用户名
#3
site01-dr ansible_ssh_host=site01.dr ansible_ssh_port=65422 #通过ansible_ssh_host改变主机名,ansible_ssh_port参数可以改变默认的远程ssh连接端口
[production]
#5
site01
#6
site02
#7
db01 ansible_ssh_user=fred #改变连接远程受管机器时使用的用户名
ansible_ssh_private_key_file=/home/fred/.ssh.id_rsa bastion #指定连接时使用的ssh私钥文件
如果你不习惯使用root来连接远程受管机器,或者你的os版本不运行root登录(比如ubuntu、dabian),那么你可以制定使用sudo来获得root权限。这个配置和更改主机端口一样简单,但是你需要在受管机器上面配置sudo:
第一步,你要在/etc/sudoers文件中添加一行,如果你选择使用你现在的用户的话,他应该已经存在了。你可以选择是否需要密码,如果选择使用密码的话,在Ansible的命令行中就需要加入-k参数,或者设置ask_sudo_pass 为ture在/etc/ansible/ansible.cfg文件中,在Ansible命令行中使用sudo需要在命令行中添加--sudo。
First steps with Ansible Ansible modules take arguments in key value pairs that look similar to key=value , perform a job on the remote server, and return information about the job as JSON. The key value pairs allow the module to know what to do when requested. They can be hard coded values, or in playbooks they can use variables, which will be covered in Chapter 2, Simple Playbooks. The data returned from the module lets Ansible know if anything changed or if any variables should be changed or set afterwards. Modules are usually run within playbooks as this lets you chain many together, but they can also be used on the command line. Previously, we used the ping command to check that Ansible had been correctly setup and was able to access the configured node. The ping module only checks that the core of Ansible is able to run on the remote machine but effectively does nothing. A slightly more useful module is called setup. This module connects to the configured node, gathers data about the system, and then returns those values. This isn't particularly handy for us while running from the command line, however, in a playbook you can use the gathered values later in other modules. To run Ansible from the command line, you need to pass two things, though usually three. First is a host pattern to match the machine that you want to apply the module to. Second you need to provide the name of the module that you wish to run and optionally any arguments that you wish to give to the module. For the host pattern, you can use a group name, a machine name, a glob, and a tilde (~), followed by a regular expression matching hostnames, or to symbolize all of these, you can either use the word all or simply * . To run the setup module on one of your nodes, you need the following command line: $ ansible machinename -u root -k -m setup The setup module will then connect to the machine and give you a number of useful facts back. All the facts provided by the setup module itself are prepended with ansible_ to differentiate them from variables. The following is a table of the most common values you will use, example values, and a short description of the fields:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shouji/article-49614-5.html
敌动
人家是公民
这砖家算是在宣传反动