ansible set_fact 在playbook中动态添加变量 (register也可以)

set_fact

    set_fact模块可以自定义facts,这些自定义的facts可以通过template或者变量的方式在playbook中使用。如果你想要获取一个进程使用的内存的百分比,则必须通过set_fact来进行计算之后得出其值,并将其值在playbook中引用。

下面是一个配置mysql innodb buffer size的示例:

- name: Configure MySQL

  hosts: mysqlservers

  tasks: 

    - name: install MySql

      yum: name=mysql-server state=installed

 

    - name: Calculate InnoDB buffer pool size

      set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}"

 

    - name: Configure MySQL 

      template: src=templates/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 

      notify: restart mysql 

 

    - name: Start MySQL 

      service: name=mysqld state=started enabled=yes 

  handlers: 

    - name: restart mysql 

      service: name=mysqld state=restarted


my.cnf的配置示例:

# {{ ansible_managed }}

[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

参考:https://www.cnblogs.com/paul8339/p/6159228.html

你可能感兴趣的:(ansible)