gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:09:33 27 4
gpt4 key购买 nike

我正在尝试使用 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 文件,我希望它登录到 dog 环境,告诉我它在那里,登录到 cat 环境,告诉我它在那里。然后,如果我有马、 Frog 或任何环境,我可以继续添加到 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

关于ansible - 使ansible根据变量多次运行剧本中的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com