根据变量多次使 ansible 运行 成为剧本中的任务

Make ansible run a task in a playbook multiple times based on variables

我正在尝试使用 Ansible URI 模块登录多个网页并检查环境是否正常运行。

目前,我希望它只登录 2 个网页(Peoplesoft envs),但我想要一个 vars 文件,每次我想查看新页面时都可以添加它。

这是我目前所拥有的,但它似乎并没有登录到两个页面,只有其中一个页面....任何帮助将不胜感激。

剧本 -

---
- name: Run Selenium Test Scripts
hosts: local
vars_files:
  - /etc/ansible/uri_module/vars_uri.yml

tasks:

  - name: Installing URI dependancy
    yum: name=python-httplib2.noarch state=present

  - name: Log into Webpage
    uri:
      url: http://{{appserver}}:{{port}}/{{dbname}}/signon.html
      method: POST
      body: "name={{userid}}&password={{password}}&enter=Sign%20in"
      with_file: /etc/ansible/uri_module/vars_uri.yml

变量文件

---
  - { name: 'dog', appserver: 'st1920', port: '8100', dbname: 'dbdog', userid: 'user', password: 'pass' }
  - { name: 'cat', appserver: 'st1921', port: '8300', dbname: 'dbcat', userid: 'user', password: 'pass' }

用-vvvv输出

ok: [local] => {"changed": false, "content_language": "en-US", "content_length": "1831", "content_type": "text/html", "date": "Thu, 13 Oct 2016 11:45:23 GMT", "invocation": {"module_args": {"backup": null, "body": "name=user&password=pass&enter=Sign%20in", "body_format": "raw", "content": null, "creates": null, "delimiter": null, "dest": null, "directory_mode": null, "follow": false, "follow_redirects": "safe", "force": null, "force_basic_auth": false, "group": null, "method": "POST", "mode": null, "owner": null, "password": null, "regexp": null, "remote_src": null, "removes": null, "return_content": false, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "status_code": [200], "timeout": 30, "url": "http://st1921:8300/dbcat/signon.html", "user": null, "validate_certs": true, "with_file": "/etc/ansible/uri_module/vars_uri.yml"}, "module_name": "uri"}, "last_modified": "Wed, 13 Aug 2014 11:42:42 GMT", "redirected": false, "server": "WebSphere Application Server/7.0", "status": 200}    

使用 vars 文件,我希望它登录狗环境,告诉我它在那里,登录猫环境,告诉我它在那里。然后,如果我有马、青蛙或任何环境,我可以继续添加到 vars 文件,而无需添加或更改剧本。目前它只登录猫,我不知道为什么。 我用这个走了正确的路线吗?有更好的方法吗?由于它没有给出错误,我正在努力找出问题所在! 谢谢。

我认为这样使用 with_file 是不可能的。

vars_uri 字典存储在这样的列表中会更干净:

---

vars_uri:
   - { name: 'dog', appserver: 'st1920', port: '8100', dbname: 'dbdog', userid: 'user', password: 'pass' }
   - { name: 'cat', appserver: 'st1921', port: '8300', dbname: 'dbcat', userid: 'user', password: 'pass' }

并用 with_items 循环遍历它,如下所示:

---
# Run Selenium Test Scripts
hosts: local
vars_files:
  - /etc/ansible/uri_module/vars_uri.yml

tasks:

  - name: Installing URI dependancy
    yum: name=python-httplib2.noarch state=present

  - name: Log into Webpage
    uri:
      url: http://{{ item.appserver }}:{{ item.port }}/{{ item.dbname }}/signon.html
      method: POST
      body: "name={{ item.userid }}&password={{ item.password }}&enter=Sign%20in"
    with_items: vars_uri