代码之家  ›  专栏  ›  技术社区  ›  Bidyut

基于Ansible中的索引将EC2主机添加到不同的组中

  •  3
  • Bidyut  · 技术社区  · 9 年前

    我正在尝试使用EC2中的Ansible设置Spark cluster。以下是创建服务器的任务,在创建服务器后,我想将第一个服务器的公共IP添加到组“master”中,将其余服务器添加到“slaves”中:

      tasks:
         - name: creating public instance
           ec2:
             aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
             aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
             instance_type: "t2.micro"
             instance_tags: { 'name': 'test' }
             image: "ami-936d9d93"
             count: "{{ count }}"
             wait: yes
             group_id: "sg-47230b20"
             region: "ap-northeast-1"
             state: present
             vpc_subnet_id: "subnet-f4c674ac"
             assign_public_ip: yes
             key_name: "test-key"
           register: public
           - name: adding host to master
             add_host:
               name: "{{ public.instances[0]['public_ip'] }}"
               groups: master
               ansible_user: ubuntu
           - name: adding host to slaves
             add_host: 
               name: "{{ public.instances[item]['public_ip'] }}"
               groups: slaves
               ansible_user: ubuntu
            with_sequence: start=1 end=3 stride=1
    

    {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute u'1'

    我不知道错误在哪里,有谁能帮我解决问题吗。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Konstantin Suvorov    9 年前

    你可以试试 slicing

      - name: adding host to slaves
         add_host: 
           name: "{{ item.public_ip }}"
           groups: slaves
           ansible_user: ubuntu
        with_items: "{{ public.instances[1:] }}"