当前位置: 首页 > 知识库问答 >
问题:

我如何在没有指定主机的情况下运行Ansible playbook?

毋炳
2023-03-14

我正在编写一个剧本,它旋转X个EC2 AWS实例,然后在它们上安装一些软件(apt包和pip模块)。当我运行playbook时,它会在我的本地系统上执行shell命令,因为Ansible不会运行,除非我指定一个主机并放置localhost。

在playbook中,我尝试在顶层指定“hosts:all”,但这只会让playbook运行一秒钟而不做任何事情。

- name: Spin up spot instances
  hosts: localhost
  connection: local
  vars_files: ansible-vars.yml
  tasks:
    - name: create {{ spot_count }} spot instances with spot_price of ${{ spot_price }}      
      local_action:
        module: ec2
        region: us-east-2
        spot_price:  '{{ spot_price }}'
        spot_wait_timeout: 180
        keypair: My-keypair
        instance_type: t3a.nano
        image: ami-0f65671a86f061fcd
        group: Allow from Home
        instance_initiated_shutdown_behavior: terminate
        wait: yes
        count:  '{{ spot_count }}'
      register: ec2

    - name: Wait for the instances to boot by checking the ssh port
      wait_for: host={{item.public_ip}} port=22 delay=15 timeout=300 state=started
      with_items: "{{ ec2.instances }}"

    - name: test whoami
      args:
        executable: /bin/bash
      shell: whoami
      with_items: "{{ ec2.instances }}"

    - name: Update apt
      args:
        executable: /bin/bash
      shell: apt update
      become: yes
      with_items: "{{ ec2.instances }}"

    - name: Install Python and Pip
      args:
        executable: /bin/bash
      shell: apt install python3 python3-pip -y        
      become: yes
      with_items: "{{ ec2.instances }}"

    - name: Install Python modules
      args:
        executable: /bin/bash
      shell: pip3 install bs4 requests
      with_items: "{{ ec2.instances }}"

ansible_ssh_private_key_file: ~/.ssh/my-keypair.pem
spot_count: 1
spot_price: '0.002'
remote_user: ubuntu

EC2实例创建得很好,“wait for ssh”任务可以工作,但是shell任务在本地系统上运行,而不是在远程主机上运行。

既然我们是在动态创建EC2实例,我如何告诉Ansible在不使用主机文件的情况下连接到EC2实例呢?

共有1个答案

帅锦
2023-03-14

如果有用的话你能试试这个吗。

- name: test whoami
      args:
        executable: /bin/bash
      shell: whoami
      delegate_to: "{{ item }}"
      with_items: "{{ ec2.instances }}"
 类似资料:
  • 问题内容: 有人建议可以怎一个JAVA程序运行不写一个主要方法.. 例如: 不使用类中的公共静态void main(String arg []),如何在控制台上打印以上行。 问题答案: 直到JDK6,您都可以使用静态初始化程序块来打印消息。这样,一旦您的班级被加载,消息就会被打印出来。然后,技巧就变成了使用另一个程序来加载您的类。 当然,您可以按如下方式运行程序,您将看到消息;但是,该命令也会失败

  • 问题内容: 我想知道像Karel这样的程序可以运行机器人,因为它没有使用main()方法。相反,它使用run()方法: 它是如何工作的? 问题答案: 实际的主要方法在其他地方。例如,在KarelRunner类中。当Java执行程序时,它实际上是在运行程序类中执行main方法。从该运行程序代码中调用您自己的run方法。

  • 文档说这个库运行在GPU上。如果我功能强大的笔记本电脑没有GPU,我还能运行Deeplearning4J吗?

  • 我只需要"解除屏蔽"我的终端后...有一些选项或setpup为它? 注意:我的服务正在使用,因此假设不需要在命令行上使用。 PS:我需要保持容器运行,我不需要使用

  • 我有一个Spring boot项目,它有使用kafka进行日志记录的依赖项。我无法摆脱所述依赖项,因为我的逻辑需要来自该依赖项的代码 我已尝试禁用自动配置 我尝试通过bean配置将missingTopicsFatal设置为false 但很明显,设置已经是假的,因为我的应用程序运行时没有失败,只是tomcat不想打开侦听端口 应用程序启动,但spring拒绝打开侦听端口,并继续使用错误进行循环 在本

  • 问题内容: 我想像运行shell命令: 不用任何插件就能做到吗?由于Jenkins不是,而是服务帐户,我该如何添加? 问题答案: 首先执行 然后执行 然后注销 对于注销很重要,因为您需要重新评估组成员身份 登录并重试 有用!