我们如何在配置文件中使用新启动的机器IP

时间:2019-01-23 06:24:10

标签: ansible

我正在使用ansible在Digital Ocean中创建Droplet并在启动时对其进行配置。一切正常。

现在我需要在我的一个配置文件中使用新创建的服务器IP,因此我想在启动时配置中也进行此更改,以避免手动更改。

我认为可以通过在“变量”中获取服务器的IP地址来实现,之后我可以在lineinfile模块中使用此变量来替换我的配置文件中的一行。

有人可以帮助我如何将服务器IP存储在变量中吗?

下面是我的ansible-playbook

---
- hosts: localhost
  tasks:
  - name: Create new DO Droplet
    digital_ocean:
     state: present
     command: droplet
     name: ansibletest
     api_token: xyz
     size_id: '1gb'
     region_id: ams3
     image_id: '41695378'
     ssh_key_ids: '23625200'
    register: my_droplet
  - name: print info about my_droplet
    local_action:
      module:  debug
         msg= "ID is {{ my_droplet.droplet.id }} IP is {{  my_droplet.droplet.ip_address }}"
  - name: Add new droplet to host group
    local_action: add_host hostname={{ my_droplet.droplet.ip_address }} groupname=launched
  - name: Wait for SSH to come up
    local_action: wait_for host={{ my_droplet.droplet.ip_address }} port=22 delay=60 timeout=320 state=started
- hosts: launched
  remote_user: root
  become: true
  gather_facts: no
  tasks:
  - name: installing python for dependencies
    raw: |
     set -e
     if [ -x /usr/bin/python ]; then exit 0; fi
     export DEBIAN_FRONTEND=noninteractive
     apt-get update
     apt-get install -y python
  - name: update repo
    apt:
     update_cache: yes
  - name: install the list of below packages
    apt: name={{ item }} update_cache=true
    with_items:  
        - build-essential
        - tcl
        - libjemalloc-dev
  - name: downoad and unarchive the redis file
    unarchive: src=http://download.redis.io/redis-stable.tar.gz dest=/home copy=no
  - name: run make command to install redis
    command: "{{ item }} chdir=/home/redis-stable"
    with_items:
        - make
        - make test
        - make install
  - name: ufw enable
    ufw: 
    state: enabled
     policy: allow
  - name: open ufw and allow port
    ufw:
     rule: allow
     proto: tcp
     port: 22
  - name: copy redis.conf file
    copy:
     src: /etc/ansible/redis.conf 
     dest: /home/redis-stable/redis.conf
  - name: copy sentinel.conf
    copy:
     src: /etc/ansible/sentinel.conf
     dest: /home/redis-stable/sentinel.conf
  - name: making change in the sentinel.conf file
    lineinfile:
     path: /etc/ansible/sentinel.conf
     regexp: '^sentinel monitor'
     line: 'sentinel monitor mymaster {{ my_droplet.droplet.ip_address }} 6379 2' 

以下是我的错误:

"msg": "The task includes an option with an undefined variable. The error was: 'my_droplet' is undefined

任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

假定是这样的语句:

line: 'sentinel monitor mymaster {{ my_droplet.droplet.ip_address }} 6379 2'

在引起问题的第二局中,注册的变量不会在局之间直接存在。另外,即使他们这样做了,您的第一部戏也和第二部戏中的那些主机不在同一个主机上。已注册变量的实例仅对其所注册的主机直接可用。

但是,可以通过hostvars系统访问已注册的变量。这允许一个主机访问与另一主机关联的变量,并且通常用于访问事实系统中收集的事实。通过此方法也可以使用注册的变量。基本形式是:

line: 'sentinel monitor mymaster {{ hostvars['some_host_in_your_inventory'].variable_name }} 6379 2'

line: 'sentinel monitor mymaster {{ hostvars['some_host_in_your_inventory']['variable_name'] }} 6379 2'

在特定情况下,您要访问在控制计算机上注册的'my_droplet'变量('localhost')。这可能不会出现在清单文件中,但是您总是会自动免费获得localhost。因此,请在您的代码中尝试以下操作:

line: 'sentinel monitor mymaster {{ hostvars['localhost'].my_droplet.droplet.ip_address }} 6379 2'