6、变量的使用

6.1、介绍

  1. 变量名:仅能由字母、数字和下划线组成,且只能以字母开头
  2. 变量来源:
  3. 1> ansible setup facts 远程主机的所有变量都可直接调用 (系统自带变量)
  4. setup模块可以实现系统中很多系统信息的显示
  5. 可以返回每个主机的系统信息包括:版本、主机名、cpu、内存
  6. ansible all -m setup -a 'filter="ansible_nodename"' 查询主机名
  7. ansible all -m setup -a 'filter="ansible_memtotal_mb"' 查询主机内存大小
  8. ansible all -m setup -a 'filter="ansible_distribution_major_version"' 查询系统版本
  9. ansible all -m setup -a 'filter="ansible_processor_vcpus"' 查询主机cpu个数
  10. 2> 在/etc/ansible/hosts(主机清单)中定义变量
  11. 普通变量:主机组中主机单独定义,优先级高于公共变量(单个主机 )
  12. 公共(组)变量:针对主机组中所有主机定义统一变量(一组主机的同一类别)
  13. 3> 通过命令行指定变量,优先级最高
  14. ansible-playbook e varname=value
  15. 4> playbook中定义
  16. vars:
  17. - var1: value1
  18. - var2: value2
  19. 5> 在独立的变量YAML文件中定义
  20. 6> role中定义
  21. 变量命名:
  22. 变量名仅能由字母、数字和下划线组成,且只能以字母开头
  23. 变量定义:key=value
  24. 示例:http_port=80
  25. 变量调用方式:
  26. 1> 通过{{ variable_name }} 调用变量,且变量名前后必须有空格,有时用“{{ variable_name }}”才生效
  27. 2> ansible-playbook e 选项指定
  28. ansible-playbook test.yml -e "hosts=www user=magedu"

6.2、主机清单中定义变量

  1. 在主机清单中定义变量,在ansible中使用变量
  2. vim /etc/ansible/hosts
  3. [appsrvs]
  4. 192.168.38.17 http_port=817 name=www
  5. 192.168.38.27 http_port=827 name=web
  6. 调用变量
  7. ansible appsrvs -m hostname -a'name={{name}}' 更改主机名为各自被定义的变量
  8. 针对一组设置变量
  9. [appsrvs:vars]
  10. make="-"
  11. ansible appsrvs -m hostname -a 'name={{name}}{{mark}}{{http_port}}' ansible调用变量

6.3、playbook中引用变量文件

  1. 将变量写进单独的配置文件中引用
  2. vim vars.yml
  3. pack: vsftpd
  4. service: vsftpd
  5. playbookl引用变量文件
  6. - hosts: websrvs
  7. remote_user: root
  8. vars_files:
  9. - vars.yml #相对路径 绝对路径

6.4、示例

  1. Facts:是由正在通信的远程目标主机发回的信息,这些信息被保存在ansible变量中。
  2. 要获取指定的远程主机所支持的所有facts,可使用如下命令进行
  3. ansible websrvs -m setup
  4. 通过命令行传递变量
  5. 在运行playbook的时候也可以传递一些变量供playbook使用
  6. 示例:
  7. ansible-playbook test.yml -e "hosts=www user=magedu"
  8. register
  9. 把任务的输出定义为变量,然后用于其他任务
  10. 示例:
  11. tasks:
  12. - shell: /usr/bin/foo
  13. register: foo_result
  14. ignore_errors: True

6.4.1、示例:使用setup变量

  1. 示例:var.yml
  2. - hosts: websrvs
  3. remote_user: root
  4. tasks:
  5. - name: create log file
  6. file: name=/var/log/ {{ ansible_fqdn }} state=touch
  7. ansible-playbook var.yml

6.4.2、示例:-e 参数传变量

  1. 示例:var.yml
  2. - hosts: websrvs
  3. remote_user: root
  4. tasks:
  5. - name: install package
  6. yum: name={{ pkname }} state=present
  7. ansible-playbook e pkname=httpd var.yml

6.4.3、示例:playbook中定义变量

  1. 示例:var.yml
  2. - hosts: websrvs
  3. remote_user: root
  4. vars:
  5. - username: user1
  6. - groupname: group1
  7. tasks:
  8. - name: create group
  9. group: name={{ groupname }} state=present
  10. - name: create user
  11. user: name={{ username }} state=present
  12. ansible-playbook var.yml
  13. ansible-playbook -e "username=user2 groupname=group2” var2.yml

6.4.4、主机清单中变量

  1. 主机变量
  2. 可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用
  3. 示例:
  4. [websrvs]
  5. www1.magedu.com http_port=80 maxRequestsPerChild=808
  6. www2.magedu.com http_port=8080 maxRequestsPerChild=909
  7. 组变量
  8. 组变量是指赋予给指定组内所有主机上的在playbook中可用的变量
  9. 示例:
  10. [websrvs]
  11. www1.magedu.com
  12. www2.magedu.com
  13. [websrvs:vars]
  14. ntp_server=ntp.magedu.com
  15. nfs_server=nfs.magedu.com
  1. 普通变量
  2. [websrvs]
  3. 192.168.99.101 http_port=8080 hname=www1
  4. 192.168.99.102 http_port=80 hname=www2
  5. 公共(组)变量
  6. [websvrs:vars]
  7. http_port=808
  8. mark="_"
  9. [websrvs]
  10. 192.168.99.101 http_port=8080 hname=www1
  11. 192.168.99.102 http_port=80 hname=www2
  12. ansible websvrs m hostname a name={{ hname }}{{ mark }}{{ http_port }}’
  13. 命令行指定变量:
  14. ansible websvrs e http_port=8000 m hostname a'name={{ hname }}{{ mark }}{{ http_port }}'

6.4.5、使用变量文件

  1. cat vars.yml
  2. var1: httpd
  3. var2: nginx
  4. cat var.yml
  5. - hosts: web
  6. remote_user: root
  7. vars_files:
  8. - vars.yml
  9. tasks:
  10. - name: create httpd log
  11. file: name=/app/{{ var1 }}.log state=touch
  12. - name: create nginx log
  13. file: name=/app/{{ var2 }}.log state=touch
  14. hostname app_81.magedu.com hostname 不支持"_",认为"_"是非法字符
  15. hostnamectl set-hostname app_80.magedu.com 可以更改主机名

6.5、组嵌套:变量

  1. 组嵌套
  2. inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。
  3. 这些变量只能在ansible-playbook中使用,而ansible命令不支持
  4. 示例:
  5. [apache]
  6. httpd1.magedu.com
  7. httpd2.magedu.com
  8. [nginx]
  9. ngx1.magedu.com
  10. ngx2.magedu.com
  11. [websrvs:children]
  12. apache
  13. nginx
  14. [webservers:vars]
  15. ntp_server=ntp.magedu.com

6.6、invertory参数

  1. invertory参数:用于定义ansible远程连接目标主机时使用的参数,而非传递给playbook的变量
  2. ansible_ssh_host
  3. ansible_ssh_port
  4. ansible_ssh_user
  5. ansible_ssh_pass
  6. ansbile_sudo_pass
  7. 示例:
  8. cat /etc/ansible/hosts
  9. [websrvs]
  10. 192.168.0.1 ansible_ssh_user=root ansible_ssh_pass=magedu
  11. 192.168.0.2 ansible_ssh_user=root ansible_ssh_pass=magedu

6.6.1、invertory参数

  1. inventory参数
  2. ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;
  3. 这些参数如下所示:
  4. ansible_ssh_host
  5. The name of the host to connect to, if different from the alias you wishto give to it.
  6. ansible_ssh_port
  7. The ssh port number, if not 22
  8. ansible_ssh_user
  9. The default ssh user name to use.
  10. ansible_ssh_pass
  11. The ssh password to use (this is insecure, we strongly recommendusing --ask-pass or SSH keys)
  12. ansible_sudo_pass
  13. The sudo password to use (this is insecure, we strongly recommendusing --ask-sudo-pass)
  14. ansible_connection
  15. Connection type of the host. Candidates are local, ssh or paramiko.
  16. The default is paramiko before Ansible 1.2, and 'smart' afterwards which
  17. detects whether usage of 'ssh' would be feasible based on whether
  18. ControlPersist is supported.
  19. ansible_ssh_private_key_file
  20. Private key file used by ssh. Useful if using multiple keys and you don't want to use SSH agent.
  21. ansible_shell_type
  22. The shell type of the target system. By default commands are formatted
  23. using 'sh'-style syntax by default. Setting this to 'csh' or 'fish' will cause
  24. commands executed on target systems to follow those shell's syntax instead.
  25. ansible_python_interpreter
  26. The target host python path. This is useful for systems with more
  27. than one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/python
  28. is not a 2.X series Python. We do not use the "/usr/bin/env" mechanism as that requires the remote user's
  29. path to be set right and also assumes the "python" executable is named python,where the executable might
  30. be named something like "python26".
  31. ansible\_\*\_interpreter
  32. Works for anything such as ruby or perl and works just like ansible_python_interpreter.
  33. This replaces shebang of modules which will run on that host.