2017-02-15 41 views
2

我試圖在AWS上配置新的機器與ec2模塊 並更新我的主機文件在本地,以便下一個任務已經使用主機文件。負責任的動態主機拒絕使用自定義解釋器

因此,配置是不是和問題,本地主機文件,甚至創造:

- name: Provision a set of instances 
     ec2: 
     key_name: AWS 
     region: eu-west-1 
     group: default 
     instance_type: t2.micro 
     image: ami-6f587e1C# For Ubuntu 14.04 LTS use ami-b9b394ca # For Ubuntu 16.04 LTS use ami-6f587e1c 
     wait: yes 
     volumes: 
      - device_name: /dev/xvda 
      volume_type: gp2 
      volume_size: 50 
      wait: true 
     count: 2 
     vpc_subnet_id: subnet-xxxxxxxx 
     assign_public_ip: yes 
     instance_tags: 
      Name: Ansible 
     register: ec2 

    - name: Add all instance private IPs to host group 
     add_host: 
      hostname: "{{ item.private_ip }}" 
      ansible_ssh_user: ubuntu 
      groups: aws 
     with_items: "{{ ec2.instances }}" 

    - local_action: file path=./hosts state=absent 
     ignore_errors: yes 

    - local_action: file path=./hosts state=touch 

    - local_action: lineinfile line="[all]" insertafter=EOF dest=./hosts 

    - local_action: lineinfile line="{{ item.private_ip }} ansible_python_interpreter=/usr/bin/python3" insertafter=EOF dest=./hosts 
     with_items: "{{ ec2.instances }}" 

    - name: Wait for SSH to come up 
     wait_for: 
      host: "{{ item.private_ip }}" 
      port: 22 
      delay: 60 
      timeout: 600 
      state: started 
     with_items: "{{ ec2.instances }}" 

    - name: refreshing inventory cache 
     meta: refresh_inventory 

- hosts: all 
    gather_facts: False 
    tasks: 
    - command: hostname -i 

但是接下來的任務是主機名-i的一個簡單的打印(只用於測試) 失敗因爲它無法在Ubuntu 16.04找到LTS的Python 2.7(有python3) 對於這一點,在我的動態主機文件,我添加以下行:

ansible_python_interpreter=/usr/bin/python3 

但似乎ansible不理它,直接轉到python 2.7缺失。

我試圖重新加載清單文件

meta: refresh_inventory 

,但沒有任何幫助。 我在做什麼錯?

+0

注意在ansible蟒蛇3的支持仍然是一個有點片狀調試:https://docs.ansible.com/ansible/python_3_support。 html – SztupY

+0

@SztupY - 是的,我知道,我只是想知道其他人如果需要使用不包含python2.x的Ubuntu 16.04? –

回答

1

我不知道爲什麼刷新不起作用,但我建議將它設置在add_host部分,它可以使用任何變量。

- name: Add all instance private IPs to host group 
    add_host: 
     hostname: "{{ item.private_ip }}" 
     ansible_ssh_user: ubuntu 
     groups: aws 
     ansible_python_interpreter: "/usr/bin/python3" 
    with_items: "{{ ec2.instances }}" 

而且我發現它有用此任務

- debug: var=hostvars[inventory_hostname]