Ansible初级应用

安装

$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
$ source ./hacking/env-setup
$ sudo pip install paramiko PyYAML Jinja2 httplib2 six

说明:一旦运行env-setup脚本,就意味着Ansible从源码中运行起来了.默认的inventory文件是 /etc/ansible/hosts.inventory,如需修改可执行如下命令

$ echo "127.0.0.1" > ~/.ansible_hosts
$ export ANSIBLE_HOSTS=~/.ansible_hosts 
//echo "127.0.0.1" > /etc/ansible/hosts
//export ANSIBLE_HOSTS=/etc/ansible/hosts

添加 客户端主机

$ vim ~/.ansible_hosts   添加172.19.2.150 
$ ansible all -m ping    利用当前用户名来连接所有客户端
$ ansible all -m ping -u sim5scm  以sim5scm账户ping所有客户端
$ ansible all -m ping -u bruce --sudo  利用bruce账户访问所有客户端的sudo模式
$ ansible 172.19.2.150 -m ping -u sim5scm  利用sim5scm账户链接172.19.2.150
$ ansible 172.19.2.150 -a "/bin/echo hello" -u sim5scm  运行命令

如果出现如下错误:

172.19.2.161 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).
", 
    "unreachable": true
}

则可以将本机的公约添加到对应服务器(172.19.2.161 .ssh/authorized_keys)

Inventory文件

$ vim /etc/ansible/hosts

127.0.0.1

[CompileServers]
172.19.2.150
172.19.2.161

172.19.2.160:80
www[01:50].example.com

[targets]
localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_ssh_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_ssh_user=mdehaan

//主机变量
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

//组变量
[atlanta]
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

//将一个组作为另一组的子组
[atlanta]
host1
host2

[raleigh]
host2
host3

[southeast:children]
atlanta
raleigh

[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

[usa:children]
southeast
northeast
southwest
northwest

说明:

  • 方括号[]中是组名,用于对系统进行分类,便于对不同系统进行个别的管理.

  • 一个系统可以属于不同的组,这时属于两个组的变量都可以为这台主机所用

  • 如果有主机的SSH端口不是标准的22端口,可在主机名之后加上端口号,用冒号分隔.SSH 配置文件中列出的端口号不会在 paramiko 连接中使用,会在 openssh 连接中使用.

  • 数字的简写模式中,01:50 也可写为 1:50,意义相同,表示一组相似的hostname

  • 选择连接类型和连接用户名(如targets组)

Inventory 参数的说明:

ansible_ssh_host
      将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.

ansible_ssh_port
      ssh端口号.如果不是默认的端口号,通过此变量设置.

ansible_ssh_user
      默认的 ssh 用户名

ansible_ssh_pass
      ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)

ansible_sudo_pass
      sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
      sudo 命令路径(适用于1.8及以上版本)

ansible_connection
      与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.

ansible_ssh_private_key_file
      ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.

ansible_shell_type
      目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.

ansible_python_interpreter
      目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  *BSD, 或者 /usr/bin/python
      不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).

      与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

来源:华为云社区  作者:烟花易冷

原文地址:https://www.cnblogs.com/2020-zhy-jzoj/p/13165845.html