ansible变量
程序员文章站
2023-04-05 09:55:05
[toc] ansible变量 定义变量的方式 命令行 playbook 主机清单 如何定义变量 1.在playbook中定义变量 2.使用vars_files定义变量 3.使用ansible内置变量 4.使用主机清单定义变量 调用变量 5.官方推荐方式 6.命令行定义变量 ansible变量优先级 ......
目录
ansible变量
定义变量的方式
- 命令行
- playbook
- 主机清单
如何定义变量
1.在playbook中定义变量
#方法一: - hosts: db02 vars: pkg: - mysql-python - mariadb-server tasks: - name: yum: name: "{{ pkg }}" state: present #方法二: - hosts: db01 vars: web_pkg: httpd db_pkg: - unzip - vsftpd php_pkg: - php-fpm - php - php-mysql
2.使用vars_files定义变量
#定义阶段 [root@m01 project]# vim vars_file.yml web_pkg: httpd db_pkg: mariadb-server php_pkg: php #调用阶段 [root@m01 project]# vim httpd.yml - hosts: web_group vars_files: ./vars_file.yml tasks: - name: install httpd yum: name: "{{ db_pkg }}" state: present
3.使用ansible内置变量
vim mkdri.yml - hosts: web_group tasks: - name: mkdir file: path: /backup/{{ ansible_fqdn }}_{{ ansible_default_ipv4['address'] }}_{{ ansible_date_time['date'] }} state: directory recurse: yes
4.使用主机清单定义变量
#定义阶段 [webs] zls_web01 ansible_ssh_host=172.16.1.7 zls_web02 ansible_ssh_host=172.16.1.8 [webs:vars] web_pkg=nginx [backup_group] backup ansible_ssh_host=172.16.1.41 [db_group] db01 ansible_ssh_host=172.16.1.51 db02 ansible_ssh_host=172.16.1.52 [nfs_group] nfs ansible_ssh_host=172.16.1.31 [rsync_server:children] web_group backup_group [rsync_server:vars] web_pkg=httpd [nfs_server:children] web_group nfs_group
调用变量
- hosts: zls_web01 tasks: - name: install web server yum: name: "{{ web_pkg }}" state: present
5.官方推荐方式
#定义阶段 #创建主机组变量定义目录 mkdir group_vars [root@m01 ~]# vim group_vars/webs web_pkg: httpd #定义阶段 [root@m01 ~]# cat group_vars/webs web_pkg: httpd #定义阶段 [root@m01 ~]# cat host_vars/zls_web01 web_pkg: nginx [root@m01 ~]# vim web.yml - hosts: webs tasks: - name: install web server yum: name: "{{ web_pkg }}" state: present
6.命令行定义变量
#调用阶段 [root@m01 ~]# !vim vim web.yml - hosts: webs tasks: - name: install httpd server yum: name: - "{{ web_pkg }}" - name: install mariadb server yum: name: - "{{ db_pkg }}" #定义阶段 [root@m01 ~]# ansible-playbook web.yml -e "web_pkg=vsftpd" -e "db_pkg=unzip"
ansible变量优先级测试
- hosts: webs vars: dir: playbook_vars vars_files: ./vars_file.yml tasks: - name: create dir file: path: /opt/{{ dir }} state: directory
[root@m01 ~]# vim vars_file.yml dir: vars_file [root@m01 ~]# vim host_vars/zls_web01 dir: host_vars [root@m01 ~]# vim group_vars/webs dir: group_vars
测试阶段
[root@m01 ~]# ansible-playbook test.yml -e "dir=command_vars" [root@web01 ~]# ll /opt/ total 0 drwxr-xr-x 2 root root 6 sep 19 16:48 command_vars #2. [root@m01 ~]# ansible-playbook test.yml [root@web01 ~]# ll /opt/ total 0 drwxr-xr-x 2 root root 6 sep 19 16:48 command_vars drwxr-xr-x 2 root root 6 sep 19 16:50 vars_file #3. [root@m01 ~]# vim test.yml - hosts: webs vars: dir: playbook_vars tasks: - name: create dir file: path: /opt/{{ dir }} state: directory [root@m01 ~]# ansible-playbook test.yml [root@web01 ~]# ll /opt/ total 0 drwxr-xr-x 2 root root 6 sep 19 16:48 command_vars drwxr-xr-x 2 root root 6 sep 19 16:52 playbook_vars drwxr-xr-x 2 root root 6 sep 19 16:50 vars_file #4. - hosts: webs tasks: - name: create dir file: path: /opt/{{ dir }} state: directory [root@m01 ~]# ansible-playbook test.yml
==命令行 > vars_file > playbook vars > host_vars > group_vars==
ansible层定义变量
#编辑变量文件 [root@m01 ~]# vim vars_file.yml lamp: framework: web_package: httpd db_package: mariadb-server php_package: php lnmp: framework: web_package: nginx db_package: mysql php_package: php #编辑playbook文件 [root@m01 ~]# vim test.yml - hosts: web_group vars_files: ./vars_file.yml tasks: - name: install lamp httpd yum: name: "{{ lamp.framework.web_package }}" - name: install lamp mariadb-server yum: name: "{{ lamp.framework.db_package }}" - name: install lamp php yum: name: "{{ lamp.framework.php_package }}" #官方推荐写法 [root@m01 ~]# vim test.yml - hosts: web_group vars_files: ./vars_file.yml tasks: - name: install lamp httpd yum: name: "{{ lamp['framework']['web_package'] }}" - name: install lamp mariadb-server yum: name: "{{ lamp['framework']['db_package'] }}" - name: install lamp php yum: name: "{{ lamp['framework']['php_package'] }}"
facts基本用法
#编辑 [root@m01 ~]# vim facts.yml - hosts: web_group tasks: - name: get host info debug: msg: > hostname "{{ ansible_fqdn }}" and ip "{{ ansible_default_ipv4.address }}" #执行 [root@m01 ~]# ansible-playbook facts.yml [root@m01 ~]# ansible-playbook facts.yml play [web_group] ***************************************************************************************************************************************************************************************************************************** task [gathering facts] *********************************************************************************************************************************************************************************************************************** ok: [web02] ok: [web01] task [get host info] ************************************************************************************************************************************************************************************************************************* ok: [web01] => { "msg": "hostname \"web01\" and ip \"10.0.0.7\"\n" } ok: [web02] => { "msg": "hostname \"web02\" and ip \"10.0.0.8\"\n" } play recap *********************************************************************************************************************************************************************************************************************************** web01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- hosts: webs tasks: - name: mem file: path: /opt/{{ ansible_facts['fqdn'] }} #path: /root/{{ ansible_fqdn }} state: directory
关闭facts
[root@m01 ~]# vim facts.yml - hosts: web_group gather_facts: no #关闭信息采集 tasks:
facts生成zabbix配置文件
- hosts: web_group vars: - zabbix_server: 172.16.1.71 tasks: - name: copy zabbix agent conf template: src: ./zabbix_agentd.conf dest: /tmp/zabbix_agentd.conf
facts生成mysql配置文件
- hosts: db_group tasks: - name: install mysql server yum: name: mariadb-server state: present - name: copy mysql conf template: src: ./my.cnf dest: /etc/my.cnf [root@m01 ~]# vim /etc/my.cnf [mysqld] basedir=/usr datadir=/var/lib/mysql/ socket=/var/lib/mysql/mysql.sock log_error=/var/log/mariadb/mariadb.log innodb_buffer_pool_size={{ ansible_memtotal_mb * 0.8 }}
推荐阅读