Ansible变量

------ ## 一:变量 ### 1. 语法格式 ```shell 变量调用语法: {{ var_name }} ``` ### 2. 案例一 ​ 通过命令行传递变量:(通过--extra-vars或-e选项来传递key=value变量) ```bash [root@ansible-server ~]# ansible-playbook release.yml -e "user=starbuck" ``` ​ 传递字典: ```bash [root@ansible-server ~]# ansible-playbook conf.yml -e '{"conf81":"/root/81.conf","conf82":"/root/conf82"}' ``` ### 3.案例二 ​ 创建变量目录 ```bash [root@ansible-server ~]# mkdir /etc/ansible/vars [root@ansible-server ~]# cd /etc/ansible/vars/ ``` ​ 创建变量文件 ```bash [root@ansible-server vars]# vim file.yml src_path: /root/test/a.txt dest_path: /opt/test/ ``` ​ 创建一个测试文件 ```bash [root@ansible-server vars]# mkdir /root/test [root@ansible-server vars]# echo 123 >/root/test/a.txt ``` ​ 创建playbook引用变量文件 ```bash [root@ansible-server vars]# cd /etc/ansible/ [root@ansible-server ansible]# vim vars.yml - hosts: ansible-web1 user: root vars_files: - /etc/ansible/vars/file.yml tasks: - name: create directory file: path={{ dest_path }} mode=755 state=directory - name: copy file copy: src={{ src_path }} dest={{ dest_path }} ``` ​ 检测语法: ```bash [root@ansible-server ansible]# ansible-playbook --syntax-check vars.yml playbook: vars.yml ``` ​ 运行剧本: ```bash [root@ansible-server ansible]# ansible-playbook vars.yml ``` ### 4.案例三 ​ 文件中包含变量 ```bash [root@ansible-server ansible]# vim conf.yml - hosts: ansible-web1 remote_user: root vars: httpd: conf80: /root/80.conf conf8080: /root/8080.conf tasks: - name: task1 file: path: "{{httpd.conf80}}" state: touch - name: task2 file: path: "{{httpd.conf8080}}" state: touch ``` ​ 检测语法: ```bash [root@ansible-server ansible]# ansible-playbook --syntax-check conf.yml ``` ​ 运行剧本: ```BASH [root@ansible-server ansible]# ansible-playbook conf.yml ``` ### 5.案例四 在主机清单中定义变量 ```bash [root@ansible-server ansible]# tail -5 /etc/ansible/hosts [web] 10.0.0.111 [web:vars] conf81=ppp conf82=ooo ``` 创建剧本 ```bash [root@ansible-server ansible]# cat conf.yml - hosts: web remote_user: root tasks: - name: task1 file: path: "{{ conf81 }}" state: touch - name: task2 file: path: "{{ conf82 }}" state: touch ``` 语法检测 ```bash [root@xingdian ansible]# ansible-playbook --syntax-check conf.yml ``` 运行剧本 ```bash [root@xingdian ansible]# ansible-playbook conf.yml ``` ### 6.案例五 ​ set_fact自定义facts变量 ​ set_fact模块可以自定义facts,这些自定义的facts可以通过template或者变量的方式在playbook中使用。如果你想要获取一 个进程使用的内存的百分比,则必须通过set_fact来进行计算之后得出其值,并将其值在playbook中引用 配置mysql innodb buffer size的示例 ```yml - name: Configure MySQL hosts: web tasks: - name: Calculate InnoDB buffer pool size set_fact: innodb_buffer_pool_size_mb="{{ ansible_swapfree_mb / 2 }}" - name: nihao template: src=/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 ``` 参数说明 ```hell set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}" 通过sey_fact去定义,而不是直接执行 template: src=templates/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 template:这个是一个模板 可以传变量 ``` my.cnf文件 ```shell [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Configure the buffer pool innodb_buffer_pool_size = {{ innodb_buffer_pool_size_mb|int }}M [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid ```