6、变量的使用
6.1、介绍
变量名:仅能由字母、数字和下划线组成,且只能以字母开头变量来源: 1> ansible setup facts 远程主机的所有变量都可直接调用 (系统自带变量) setup模块可以实现系统中很多系统信息的显示 可以返回每个主机的系统信息包括:版本、主机名、cpu、内存 ansible all -m setup -a 'filter="ansible_nodename"' 查询主机名 ansible all -m setup -a 'filter="ansible_memtotal_mb"' 查询主机内存大小 ansible all -m setup -a 'filter="ansible_distribution_major_version"' 查询系统版本 ansible all -m setup -a 'filter="ansible_processor_vcpus"' 查询主机cpu个数 2> 在/etc/ansible/hosts(主机清单)中定义变量 普通变量:主机组中主机单独定义,优先级高于公共变量(单个主机 ) 公共(组)变量:针对主机组中所有主机定义统一变量(一组主机的同一类别) 3> 通过命令行指定变量,优先级最高 ansible-playbook –e varname=value 4> 在playbook中定义 vars: - var1: value1 - var2: value2 5> 在独立的变量YAML文件中定义 6> 在role中定义变量命名: 变量名仅能由字母、数字和下划线组成,且只能以字母开头变量定义:key=value 示例:http_port=80变量调用方式: 1> 通过{{ variable_name }} 调用变量,且变量名前后必须有空格,有时用“{{ variable_name }}”才生效 2> ansible-playbook –e 选项指定 ansible-playbook test.yml -e "hosts=www user=magedu"
6.2、主机清单中定义变量
在主机清单中定义变量,在ansible中使用变量vim /etc/ansible/hosts[appsrvs]192.168.38.17 http_port=817 name=www192.168.38.27 http_port=827 name=web调用变量ansible appsrvs -m hostname -a'name={{name}}' 更改主机名为各自被定义的变量 针对一组设置变量[appsrvs:vars]make="-"ansible appsrvs -m hostname -a 'name={{name}}{{mark}}{{http_port}}' ansible调用变量
6.3、playbook中引用变量文件
将变量写进单独的配置文件中引用vim vars.ymlpack: vsftpdservice: vsftpdplaybook中l引用变量文件- hosts: websrvs remote_user: root vars_files: - vars.yml #相对路径 绝对路径
6.4、示例
Facts:是由正在通信的远程目标主机发回的信息,这些信息被保存在ansible变量中。 要获取指定的远程主机所支持的所有facts,可使用如下命令进行 ansible websrvs -m setup通过命令行传递变量 在运行playbook的时候也可以传递一些变量供playbook使用 示例: ansible-playbook test.yml -e "hosts=www user=magedu"register把任务的输出定义为变量,然后用于其他任务示例:tasks:- shell: /usr/bin/foo register: foo_result ignore_errors: True
6.4.1、示例:使用setup变量
示例:var.yml- hosts: websrvs remote_user: root tasks: - name: create log file file: name=/var/log/ {{ ansible_fqdn }} state=touchansible-playbook var.yml
6.4.2、示例:-e 参数传变量
示例:var.yml- hosts: websrvs remote_user: root tasks: - name: install package yum: name={{ pkname }} state=presentansible-playbook –e pkname=httpd var.yml
6.4.3、示例:playbook中定义变量
示例:var.yml- hosts: websrvs remote_user: root vars: - username: user1 - groupname: group1 tasks: - name: create group group: name={{ groupname }} state=present - name: create user user: name={{ username }} state=presentansible-playbook var.ymlansible-playbook -e "username=user2 groupname=group2” var2.yml
6.4.4、主机清单中变量
主机变量可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用示例:[websrvs]www1.magedu.com http_port=80 maxRequestsPerChild=808www2.magedu.com http_port=8080 maxRequestsPerChild=909组变量组变量是指赋予给指定组内所有主机上的在playbook中可用的变量示例: [websrvs] www1.magedu.com www2.magedu.com [websrvs:vars] ntp_server=ntp.magedu.com nfs_server=nfs.magedu.com
普通变量 [websrvs] 192.168.99.101 http_port=8080 hname=www1 192.168.99.102 http_port=80 hname=www2公共(组)变量 [websvrs:vars] http_port=808 mark="_" [websrvs] 192.168.99.101 http_port=8080 hname=www1 192.168.99.102 http_port=80 hname=www2 ansible websvrs –m hostname –a ‘name={{ hname }}{{ mark }}{{ http_port }}’命令行指定变量: ansible websvrs –e http_port=8000 –m hostname –a'name={{ hname }}{{ mark }}{{ http_port }}'
6.4.5、使用变量文件
cat vars.ymlvar1: httpdvar2: nginxcat var.yml- hosts: web remote_user: root vars_files: - vars.yml tasks: - name: create httpd log file: name=/app/{{ var1 }}.log state=touch - name: create nginx log file: name=/app/{{ var2 }}.log state=touchhostname app_81.magedu.com hostname 不支持"_",认为"_"是非法字符hostnamectl set-hostname app_80.magedu.com 可以更改主机名
6.5、组嵌套:变量
组嵌套inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。这些变量只能在ansible-playbook中使用,而ansible命令不支持示例: [apache] httpd1.magedu.com httpd2.magedu.com [nginx] ngx1.magedu.com ngx2.magedu.com [websrvs:children] apache nginx [webservers:vars] ntp_server=ntp.magedu.com
6.6、invertory参数
invertory参数:用于定义ansible远程连接目标主机时使用的参数,而非传递给playbook的变量 ansible_ssh_host ansible_ssh_port ansible_ssh_user ansible_ssh_pass ansbile_sudo_pass示例: cat /etc/ansible/hosts [websrvs] 192.168.0.1 ansible_ssh_user=root ansible_ssh_pass=magedu 192.168.0.2 ansible_ssh_user=root ansible_ssh_pass=magedu
6.6.1、invertory参数
inventory参数ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;这些参数如下所示:ansible_ssh_hostThe name of the host to connect to, if different from the alias you wishto give to it.ansible_ssh_portThe ssh port number, if not 22ansible_ssh_userThe default ssh user name to use.ansible_ssh_passThe ssh password to use (this is insecure, we strongly recommendusing --ask-pass or SSH keys)ansible_sudo_passThe sudo password to use (this is insecure, we strongly recommendusing --ask-sudo-pass)ansible_connectionConnection type of the host. Candidates are local, ssh or paramiko.The default is paramiko before Ansible 1.2, and 'smart' afterwards whichdetects whether usage of 'ssh' would be feasible based on whetherControlPersist is supported.ansible_ssh_private_key_filePrivate key file used by ssh. Useful if using multiple keys and you don't want to use SSH agent.ansible_shell_typeThe shell type of the target system. By default commands are formattedusing 'sh'-style syntax by default. Setting this to 'csh' or 'fish' will causecommands executed on target systems to follow those shell's syntax instead.ansible_python_interpreterThe target host python path. This is useful for systems with morethan one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/pythonis not a 2.X series Python. We do not use the "/usr/bin/env" mechanism as that requires the remote user'spath to be set right and also assumes the "python" executable is named python,where the executable mightbe named something like "python26".ansible\_\*\_interpreterWorks for anything such as ruby or perl and works just like ansible_python_interpreter.This replaces shebang of modules which will run on that host.