使ansible基于变量多次在剧本中运行任务

时间:2016-10-13 11:51:00

标签: ansible ansible-playbook selenium-ide

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

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

这是我到目前为止所做的,但它似乎没有登录到两个页面,只有其中一个....任何帮助都应该被赞赏。

Playbook -

---
- 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

Vars文件

---
  - { 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文件,我想让它登录到狗环境,告诉我它,登录cat环境,告诉我它在那里。然后,如果我有马,青蛙或任何环境,我可以继续添加到vars文件,而无需添加或更改剧本。目前它只登录到猫,我不知道为什么。 我有这个走下正确的路线吗?有没有更好的方法呢?由于它没有给出错误,我正在努力找出问题! 谢谢。

1 个答案:

答案 0 :(得分:2)

我认为不可能像这样使用with_file

vars_uri dictonaries存储在这样的列表中会更清晰:

---

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