2015-10-27 32 views
0

我想配置文件推送到所有APP_SERVER推进使用Ansible

这里一个动态配置文件是我的清单文件

[app_servers] 
1.2.3.4 
5.6.7.8 

和我的配置文件看起来如下

conf 
{ 
name : configuration file 
ip : a.b.c.d 
} 

我的任务是推送这个配置文件,IP值应该替换为该服务器的IP。

例如,在1.2.3.4配置文件将是

conf 
{ 
name : configuration file 
ip : 1.2.3.4 
} 

和5.6.7.8中,这将是

conf 
{ 
name : configuration file 
ip : 5.6.7.8 
} 

我尝试使用替换命令,并使用组如下迭代,但是对于每个服务器来说,它的循环两次,因为它没有得到正确的IP替换。

- hosts: all 
    sudo: yes 
    tasks: 
    - replace: dest=/home/ubuntu/config regexp='a\.b\.c\.d' replace=' {{ hostvars[item]['inventory_hostname'] }}' 
     with_items: groups['app_servers'] 

是否有任何简单的方法来处理这个问题,让事情变得简单但动态?

回答

1

如果它只是对远程主机上现有文件的单行更改,则可以使用lineinfile

不过,你说的那个配置文件应该推到远程主机,在这种情况下,最好使用template模块,在Jinja2的模板写的文件,并推动它之前的变量来填充它。

facts gathered by Ansible获取远程主机的IP,例如从ansible_default_ipv4

本地您有config.j2

{ 
    name: configuration file 
    ip: {{ ansible_default_ipv4.address }} 
} 

任务,从模板建立文件:

- name: upload config file 
    template: 
    dest: /home/ubuntu/config 
    src: config.j2 

例如,IP 192.168.10.10远程主机上生成的文件/home/ubuntu/config将是:

{ 
    name: configuration file 
     ip: 192.168.10.10 
}