Ansbile AWS动态库存组无法匹配播放主机

时间:2016-03-12 02:47:30

标签: ansible ansible-playbook ansible-2.x

我无法让我的Ansible play String与我的动态广告资源回来的AWS动态群组相匹配。让我们打破这个问题。

鉴于hosts的输出:

ec2.py --list

我应该能够写一个与回来的一些小组相匹配的剧本:

$ ./devops/inventories/dynamic/ec2.py  --list
{
  "_meta": {
    "hostvars": {
      "54.37.213.132": {
        "ec2__in_monitoring_element": false, 
        "ec2_ami_launch_index": "0", 
        "ec2_architecture": "x86_64", 
        "ec2_client_token": "", 
        "ec2_dns_name": "ec2-52-37-203-132.us-west-2.compute.amazonaws.com", 
        "ec2_ebs_optimized": false, 
        "ec2_eventsSet": "", 
        "ec2_group_name": "", 
        "ec2_hypervisor": "xen", 
        "ec2_id": "i-d352c50b", 
        "ec2_image_id": "ami-63b25203", 
        "ec2_instance_profile": "", 
        "ec2_instance_type": "t2.micro", 
        "ec2_ip_address": "54.37.213.132", 
        "ec2_item": "", 
        "ec2_kernel": "", 
        "ec2_key_name": "peaker-v1-keypair", 
        "ec2_launch_time": "2016-03-11T20:45:44.000Z", 
        "ec2_monitored": false, 
        "ec2_monitoring": "", 
        "ec2_monitoring_state": "disabled", 
        "ec2_persistent": false, 
        "ec2_placement": "us-west-2a", 
        "ec2_platform": "", 
        "ec2_previous_state": "", 
        "ec2_previous_state_code": 0, 
        "ec2_private_dns_name": "ip-172-31-43-132.us-west-2.compute.internal", 
        "ec2_private_ip_address": "172.31.43.132", 
        "ec2_public_dns_name": "ec2-52-37-203-132.us-west-2.compute.amazonaws.com", 
        "ec2_ramdisk": "", 
        "ec2_reason": "", 
        "ec2_region": "us-west-2", 
        "ec2_requester_id": "", 
        "ec2_root_device_name": "/dev/xvda", 
        "ec2_root_device_type": "ebs", 
        "ec2_security_group_ids": "sg-824ac0e5", 
        "ec2_security_group_names": "peaker-v1-security-group", 
        "ec2_sourceDestCheck": "true", 
        "ec2_spot_instance_request_id": "", 
        "ec2_state": "running", 
        "ec2_state_code": 16, 
        "ec2_state_reason": "", 
        "ec2_subnet_id": "subnet-b96e1bce", 
        "ec2_tag_Environment": "v1", 
        "ec2_tag_Name": "peaker-v1-ec2", 
        "ec2_virtualization_type": "hvm", 
        "ec2_vpc_id": "vpc-5fe8ae3a"
      }
    }
  }, 
  "ec2": [
    "54.37.213.132"
  ], 
  "tag_Environment_v1": [
    "54.37.213.132"
  ], 
  "tag_Name_peaker-v1-ec2": [
    "54.37.213.132"
  ], 
  "us-west-2": [
    "54.37.213.132"
  ]
}

但是,当我--- # playbook - name: create s3 bucket with policy hosts: localhost gather_facts: yes tasks: - name: s3 s3: bucket: "fake" region: "us-west-2" mode: create permission: "public-read-write" register: s3_output - debug: msg="{{ s3_output }}" - name: test on remote machine hosts: ec2 gather_facts: yes tasks: - name: test on remote machine file: dest: "/home/ec2-user/test/" owner: ec2-user group: ec2-user mode: 0700 state: directory become: yes become_user: ec2-user 匹配这些播放时,很明显播放主机不匹配任何内容:

--list-hosts

1 个答案:

答案 0 :(得分:0)

快速解决您正在做的事情:

  • 将您的剧本中的hosts: localhost更改为hosts: all

如果您要将hosts: localhost保留在游戏手册中,那么它将永远无法使用动态广告资源...

如果是这样, - 你必须结合动态&静态库存。创建路径为./devops/inventories/dynamic/static.ini的文件(与ec2.py和ec2.ini位于同一级别)并放置此内容:

[localhost]
localhost

[ec2_tag_Name_peaker_v1_ec2]

[aws-hosts:children]
localhost
ec2_tag_Name_peaker_v1_ec2

之后,您将能够快速检查:

ansible -i devops/inventories/dynamic/ec2 aws-hosts -m ping

和你的剧本本身:

ansible-playbook -i devops/inventories/dynamic/ec2 \
    devops/build_and_bundle_example.yml --ask-vault-pass

注意: devops/inventories/dynamic/ec2是该文件夹的路径,但它会自动解析为具有aws-hosts群组名称访问权限的混合动态和静态广告资源。

事实上,这不是库存的最佳用途。但重要的是要理解,通过组合动态和静态库存,您只需为特定动态主机添加新的组名

ansible -i devops/inventories/dynamic/ec2 all -m debug \
    -a "var=hostvars[inventory_hostname].group_names"