Ansible EC2添加多个主机

时间:2017-11-06 04:36:26

标签: ansible

尝试动态查找和添加主机

---

- hosts: localhost
  gather_facts: no
  tasks:
    - name: Gather EC2 remote facts.
      ec2_remote_facts:
        region: 'us-east-1'
        register: ec2_remote_facts
    - name: Debug.
      debug:
        msg: "{{ ec2_remote_facts }}"
    - name: get instances for tags 
      add_host:
        name: "{{ item }}"
        group: dynamically_created_hosts
      with_items: |
        "{{ ec2_remote_facts.instances | 
            selectattr('tags.AppName', 'defined') | selectattr('tags.AppName', 'equalto', 'sql') | 
            selectattr('tags.AppType', 'defined') | selectattr('tags.AppType', 'equalto', 'infra') |
            map(attribute='private_ip_address') | list }}"

- hosts: 
  - dynamically_created_hosts
  become: yes
  become_user: root
  serial: 1
  vars_files:
    - group_vars/all
  tasks:
    - name: run command
      shell: "uname -a"

当我以详细模式运行时,我得到了关注

TASK [get instances for tags] **************************************************
task path: /Users/me/gitfork2/fornax/dynhst.yml:39
creating host via 'add_host': hostname="[u'10.112.114.241']"
changed: [localhost] => (item="[u'10.112.114.241']") => {"add_host": {"groups": ["dynamically_created_hosts"], "host_name": "\"[u'10.112.114.241']\"", "host_vars": {"group": "dynamically_created_hosts"}}, "changed": true, "invocation": {"module_args": {"group": "dynamically_created_hosts", "hostname": "\"[u'10.112.114.241']\""}, "module_name": "add_host"}, "item": "\"[u'10.112.114.241']\""}

PLAY [dynamically_created_hosts] ***********************************************

TASK [setup] *******************************************************************
<"[u'10.112.114.241']"> ESTABLISH SSH CONNECTION FOR USER: None
<"[u'10.112.114.241']"> SSH: ansible.cfg set ssh_args: (-F)(/Users/me/.ssh/config)
<"[u'10.112.114.241']"> SSH: ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled: (-o)(StrictHostKeyChecking=no)
<"[u'10.112.114.241']"> SSH: ansible_password/ansible_ssh_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)
<"[u'10.112.114.241']"> SSH: ANSIBLE_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)
<"[u'10.112.114.241']"> SSH: PlayContext set ssh_common_args: ()
<"[u'10.112.114.241']"> SSH: PlayContext set ssh_extra_args: ()
<"[u'10.112.114.241']"> SSH: EXEC ssh -C -vvv -F /Users/me/.ssh/config -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 '"[u'"'"'10.112.114.241'"'"']"' '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1509843772.58-176166317659656 `" && echo ansible-tmp-1509843772.58-176166317659656="` echo $HOME/.ansible/tmp/ansible-tmp-1509843772.58-176166317659656 `" ) && sleep 0'"'"''
fatal: ["[u'10.112.114.241']"]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
    to retry, use: --limit @./dynhst.retry

我看到奇怪的是

SSH: EXEC ssh -C -vvv -F /Users/me/.ssh/config -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 '"[u'"'"'10.112.114.241'"'"']"' '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1509843772.58-176166317659656 `" && echo ansible-tmp-1509843772.58-176166317659656="` echo $HOME/.ansible/tmp/ansible-tmp-1509843772.58-176166317659656 `" ) && sleep 0'"'"''

似乎正在尝试ssh到'"[u'"'"'10.112.114.241'"'"']"' ...似乎dynamically_created_hosts被用作字符串而不是列表

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

将(IP地址)列表传递给需要字符串的参数name

  

主机名= “[u'10.112.114.241' ]”

[ ]是列表的JSON表示(上例中的单个元素)。

如果您想要列表中的第一个地址(并且您的任何主机似乎没有更多地址),那么:

add_host:
  name: "{{ item[0] }}"
  group: dynamically_created_hosts
  with_items: ...