Bash 使用ansible在Openstack中运行服务器后运行shell命令

Bash 使用ansible在Openstack中运行服务器后运行shell命令,bash,shell,ansible,openstack,Bash,Shell,Ansible,Openstack,我将使用ansible playbook在Openstack中运行服务器 ~/ansible/roles/launch\u instance/tasks/main.yml --- - name: launch instance os_server: name: "{{ prefix }}-{{ item.name }}" state: present key_name: "{{ item.key }}" nics: &q

我将使用ansible playbook在Openstack中运行服务器

~/ansible/roles/launch\u instance/tasks/main.yml

---
- name: launch instance
  os_server:
    name: "{{ prefix }}-{{ item.name }}"
    state: present
    key_name: "{{ item.key }}"
    nics: "{{ item.nics }}"
    image: "{{ item.image }}"
    flavor: "{{ item.flavor }}"
    timeout: 400
    meta: "{{ item.meta }}"
    userdata: "{{ item.userdata }}"
  with_items: "{{ servers }}"
  register: "os_hosts"
- debug: var=os_hosts
---
prefix: icr
servers:

  - name: server-one
    image: ubuntu18_cloud
    flavor: c1m512d5
    key: my-cloud-key-pub
    nics: "port-id=70f5cba4-8d5e-4c36-9482-64645458ff7b"
    userdata: |
      #!/bin/bash -ex
      curl -o controller.cfg http://192.168.1.240/controller.cfg
    meta:
      group: ubuntu_servers
~/ansible/roles/launch\u instance/tasks/main.yml

---
- name: launch instance
  os_server:
    name: "{{ prefix }}-{{ item.name }}"
    state: present
    key_name: "{{ item.key }}"
    nics: "{{ item.nics }}"
    image: "{{ item.image }}"
    flavor: "{{ item.flavor }}"
    timeout: 400
    meta: "{{ item.meta }}"
    userdata: "{{ item.userdata }}"
  with_items: "{{ servers }}"
  register: "os_hosts"
- debug: var=os_hosts
---
prefix: icr
servers:

  - name: server-one
    image: ubuntu18_cloud
    flavor: c1m512d5
    key: my-cloud-key-pub
    nics: "port-id=70f5cba4-8d5e-4c36-9482-64645458ff7b"
    userdata: |
      #!/bin/bash -ex
      curl -o controller.cfg http://192.168.1.240/controller.cfg
    meta:
      group: ubuntu_servers
启动服务器后,我通过ssh(ssh)连接ubuntu@192.168.1.100)并希望看到从web服务器下载的文件。但是主目录是空的。 在本地,命令curl-ocontroller.cfg工作正常

调试:

ok: [localhost] => {
    "os_hosts": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
              ...
              "userdata": "#!/bin/bash -ex\ncurl -o controller.cfg http://192.168.1.240/controller.cfg\n",
              ...
            }
...
}
我还尝试:

userdata: |
      {%- raw -%}#!/bin/bash -ex
      curl -o controller.cfg http://192.168.1.240/controller.cfg{% endraw -%}
您能告诉我们在使用ansible启动服务器后如何执行shell命令吗

附言: 也没有给出积极的结果

userdata: |
      #cloud-config
      runcmd:
        - touch /test.txt

这可能对您有所帮助-如果您登录到服务器并检查检索到的
userdata
,它是否与您期望的一样?如果用
#启动
userdata
,会怎么样/bin/sh
而不是
#/bin/bash-ex
?FWIW,我刚刚尝试了同样的方法——使用一个Ubuntu 18云图像——它似乎工作得很好。