ansible 基础


1、安装ansible

yum -y install ansible

2、添加受控主机

ssh-keygen 生成公钥并拷贝至受控端主机的 /root/.ssh/authorized_keys 在/etc/ansible/hosts

# Ex 1: Ungrouped hosts, specify before any group headers.

## green.example.com
## blue.example.com
## 192.168.100.1
192.168.100.10

3、简单使用

[root@my-hz-server ansible]# ansible all -m ping
192.168.100.10 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false,
    "ping": "pong"
}

4、主机和组

# /etc/ansible/hosts
[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com
# 端口号不是默认设置时,可明确的表示为:
badwolf.example.com:5309
# 设置别名
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50

# 样例
some_host         ansible_ssh_port=2222     ansible_ssh_user=manager
aws_host          ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host      ansible_python_interpreter=/usr/local/bin/python
ruby_module_host  ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
# 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 的路径....

查看可用模块

[root@my-hz-server ~]# ansible-doc -l
fortios_router_community_list                                 Configure community lists in Fortinet's FortiOS and FortiGate                                                                                   
azure_rm_devtestlab_info                                      Get Azure DevTest Lab facts                                                                                                                     
ecs_taskdefinition                                            register a task definition in ecs                                                                                                               
avi_alertscriptconfig                                         Module for setup of AlertScriptConfig Avi RESTful Object                                                                                        
tower_receive                                                 Receive assets from Ansible Tower                                                                                                               
netapp_e_iscsi_target                                         NetApp E-Series manage iSCSI target configuration                                                                                               
azure_rm_acs                                                  Manage an Azure Container Service(ACS) instance                                                                                                 
fortios_log_syslogd2_filter                                   Filters for remote system server in Fortinet's FortiOS and FortiGate

4. ansible.cfg

/etc/ansible/ansible.cfg

[defaults]
remote_port    = 22
host_key_checking = False
log_path = /fjyl/logs/ansible/ansible.log
private_key_file = /etc/ansible/keys/my-key
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

5.目录结构

tree /etc/ansible

/etc/ansible
├── ansible.cfg
├── hosts
├── keys
│   └── my-key
└── roles