转载自joshua317博客 https://www.joshua317.com/article/24
#要部署的远程服务器ip,通过ansible.ini里面设置
- hosts: "{{ server_group }}"
vars:
var1: var1_string
tasks:
- shell: "echo var2_string"
register: shellReturn
- set_fact:
var1: "{{testvar1}}"
var2: "{{shellReturn.stdout}}"
- debug:
msg: "{{var1}} {{var2}}"
- hosts: "{{ server_group }}"
- debug: var=inventory_hostname
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- shell: "echo {{ inventory_hostname }}"
register: shellReturn
#把获取到的ip地址赋值给SERVER_IP变量
- set_fact:
SERVER_IP: "{{shellReturn.stdout}}"
举例说明
首先需要有一个.j2后缀的文件,如php.ini.j2,里面的内容有变量SERVER_IP
ENV.HOST_IP = "{{ SERVER_IP }}"
通过template模块替换如下:
- name: create {{ project }} copy php-conf
template:
src: "conf/php/php.ini.j2"
dest: "/data/conf/php/php.ini"
此时,通过jenkins发布的时候,php.ini文件的里面的ENV.HOST_IP值,已经变成了SERVER_IP对应的值。