Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible-从机器变量启动到循环中的命令的返回代码_Ansible - Fatal编程技术网

Ansible-从机器变量启动到循环中的命令的返回代码

Ansible-从机器变量启动到循环中的命令的返回代码,ansible,Ansible,我现在有一些重要的任务要做,我想我需要一些帮助 背景: 我的Ansible清单中有多台服务器,我希望能够启动一个playbook来检查服务器上运行的服务的状态,而无需为每个服务执行特定任务。 我已将机器变量配置为如下所示(例如): 目前的剧本如下: - hosts: all tasks: - name: Check native services status service: name: "{{ item.key }}" state: s

我现在有一些重要的任务要做,我想我需要一些帮助

背景: 我的Ansible清单中有多台服务器,我希望能够启动一个playbook来检查服务器上运行的服务的状态,而无需为每个服务执行特定任务。 我已将机器变量配置为如下所示(例如):

目前的剧本如下:

- hosts: all
  tasks:
    - name: Check native services status
      service:
        name: "{{ item.key }}"
        state: started
       with_dict: "{{ services }}"
       when: item.value.cmd == 0

    - name: Check others services
      shell: "{{ item.value.cmd }}"
      register: result
      with_dict: "{{ services }}"
      failed_when: result.rc != item.value.return_code
      when: item.value.cmd != 0 and item.value.cmd != ''
      ignore_errors: yes
对于使用“cmd:0”定义的服务,第一个任务将检查服务的状态,如果未启动,则启动它。 但对于某些自定义服务,“服务”模块无法帮助我,因此我需要启动命令以了解服务是否已启动。 在本例中,第二个任务正在工作,但我无法获取该任务的返回代码以启动将要启动服务的任务

我尝试添加此任务:

- name: test
  debug:
    msg: ({{ item.item.key }}) failed
  when: item.rc != 0
  with_items: "{{ result.results }}"
但是Ansible抱怨“dict”值没有任何返回码

我试着调试,但我不明白发生了什么。 例如,当我想查看任务的全局结果的内容时,我添加以下内容:

- name: test2
  debug:
    msg: "{{ item }}"
  with_items: "{{ result.results }}"
它给了我以下输出(包含我正在寻找的返回代码):

[…]
“结果时失败”:false,
“调用”:{
“模块参数”:{
“|原始参数”:“ps aux|grep/bin/custom httpd | ps-efl | grep custom|U owner | grep-v grep”,
“_使用_外壳”:正确,
“chdir”:空,
“创建”:null,
“可执行文件”:null,
“删除”:null,
“stdin”:空,
“警告”:正确
}
},
“项目”:{
“密钥”:“apache自定义”,
“价值”:{
“cmd”:“ps aux | grep/bin/custom|u httpd | ps-efl | grep自定义所有者| grep-v grep”,
“重新启动”:“回声重新启动”,
“返回代码”:0
}
},
“RC”:0,

“能够启动一个脚本,检查服务器上运行的服务的状态”,您可能需要考虑模块Services事实。


我建议编写一个shell脚本,以便为您的自定义服务返回正确的代码状态,而不是将其放入ansible中,并使用自定义服务列表从ansible调用此脚本。感谢您的重播,但是有一件事我不明白:为什么当我显示我的'result.results'的内容时,我需要的返回代码值就在我面前,但为什么它仍然不可访问?如果Ansible向我显示此变量,我应该能够使用它…请编辑问题,澄清“我无法获取该任务的返回代码以启动将要启动服务的任务”以及您的脸上出现的各种错误您不应检查任务的全局状态
item.rc
,而是
item.value.return\u code
,它用于传递给任务的每个服务。
item.value.return\u code
是一个自定义变量,我想用它来比较实际返回代码。这是此命令所需的返回码!
- name: test2
  debug:
    msg: "{{ item }}"
  with_items: "{{ result.results }}"
[...]
     "failed_when_result": false,
    "invocation": {
        "module_args": {
            "_raw_params": "ps aux | grep /bin/custom-httpd | ps -efl | grep custom_owner | grep -v grep",
            "_uses_shell": true,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "item": {
        "key": "apache-custom",
        "value": {
            "cmd": "ps aux | grep /bin/custom_httpd | ps -efl | grep custom-owner | grep -v grep",
            "restart": "echo restart",
            "return_code": 0
        }
    },
    "rc": 0, <====== here is the return code
    "start": "2018-07-09 15:44:24.859555",
    "stderr": "",
    "stderr_lines": [],
    [...]
- name: 'Test service_facts'
  hosts: localhost
  connection: local
  tasks:
    - name: populate service facts
      service_facts:
    - debug:
        var: ansible_facts.services