admin 发表于 2020-12-12 22:21:40

ansible11(触发器解决依赖关系)


vhosts.conf:
#
#
<VirtualHost *:80>
    DocumentRoot "/var/www/html/serverb"
    ServerName serverb.lab.example.com
    ErrorLog "/var/log/httpd/serverb-error.log"
    CustomLog "/var/log/httpd/serverb-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/html/wwwb"
    ServerName wwwb.lab.example.com
    ErrorLog "/var/log/httpd/wwwb-error.log"
    CustomLog "/var/log/httpd/wwwb-access_log" common
</VirtualHost>



---
- name: Copy httpd Temp Config
hosts: serverb
#force_handlers: yes
tasks:
- name: Copy httpdTemp
    copy:
      src: "./v6.d/vhosts.conf"
      dest: "/etc/httpd/conf.d/vhosts.conf"
    notify:
      - h_configfile

- name: Create Vhosts Dir
    file:
      dest: "/var/www/html/serverb"
      state: directory
    notify:
      - h_direxists_serverb

- name: Create Vhosts Dir2
    file:
      dest: /var/www/html/wwwb
      state: directory
    notify:
      - h_direxists_wwwb

- name:
    shell: '/usr/bin/xx'
    ignore_errors: yes

handlers:
- name: h_direxists_serverb
    copy:
      content: "Welcome to serverb.lab.example.com."
      dest: "/var/www/html/serverb/index.html"

- name: h_direxists_wwwb
    copy:
      content: "Welcome to wwwb.example.com."
      dest: "/var/www/html/wwwb/index.html"

- name: h_configfile
    service:
      name: httpd
      state: restarted

- name: Test Vhosts
hosts: localhost
vars:
    v_url:
      - serverb.lab.example.com
      - wwwb.lab.example.com
tasks:
- name: Connect
    uri:
      url: "http://{{ item }}"
      return_content: yes
      status_code: 200
    loop:
      "{{ v_url }}"
    register: v_result

- name: Show
    debug:
#      var: v_result
      msg:
      The result for "{{ item.content }}."
    loop:
      "{{ v_result.results }}"






$ ansible-playbookv6-1_server2.yml

PLAY *****************************************************************************************************************************

TASK ************************************************************************************************************************************
ok:

TASK ***********************************************************************************************************************************
changed:

TASK **********************************************************************************************************************************
ok:

TASK *********************************************************************************************************************************
ok:

TASK **********************************************************************************************************************************************
fatal: : FAILED! => {"changed": true, "cmd": "/usr/bin/xx", "delta": "0:00:00.004409", "end": "2020-12-12 22:16:44.974898", "msg": "non-zero return code", "rc": 127, "start": "2020-12-12 22:16:44.970489", "stderr": "/bin/sh: /usr/bin/xx: No such file or directory", "stderr_lines": ["/bin/sh: /usr/bin/xx: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

RUNNING HANDLER ****************************************************************************************************************************
changed:

PLAY ****************************************************************************************************************************************

TASK ************************************************************************************************************************************
ok:

TASK ********************************************************************************************************************************************
ok: => (item=serverb.lab.example.com)
ok: => (item=wwwb.lab.example.com)

TASK ***********************************************************************************************************************************************
ok: => (item={'content': 'Welcome to serverb.lab.example.com.', 'redirected': False, 'url': 'http://serverb.lab.example.com', 'date': 'Sat, 12 Dec 2020 14:16:50 GMT', 'server': 'Apache/2.4.37 (Red Hat Enterprise Linux)', 'last_modified': 'Sat, 12 Dec 2020 14:14:46 GMT', 'etag': '"23-5b64507fe9126"', 'accept_ranges': 'bytes', 'content_length': '35', 'connection': 'close', 'content_type': 'text/html; charset=UTF-8', 'cookies_string': '', 'cookies': {}, 'msg': 'OK (35 bytes)', 'status': 200, 'elapsed': 0, 'changed': False, 'invocation': {'module_args': {'url': 'http://serverb.lab.example.com', 'return_content': True, 'status_code': ['200'], 'force': False, 'http_agent': 'ansible-httpget', 'use_proxy': True, 'validate_certs': True, 'force_basic_auth': False, 'body_format': 'raw', 'method': 'GET', 'follow_redirects': 'safe', 'timeout': 30, 'headers': {}, 'follow': False, 'url_username': None, 'url_password': None, 'client_cert': None, 'client_key': None, 'dest': None, 'body': None, 'src': None, 'creates': None, 'removes': None, 'unix_socket': None, 'mode': None, 'owner': None, 'group': None, 'seuser': None, 'serole': None, 'selevel': None, 'setype': None, 'attributes': None, 'content': None, 'backup': None, 'remote_src': None, 'regexp': None, 'delimiter': None, 'directory_mode': None, 'unsafe_writes': None}}, 'failed': False, 'item': 'serverb.lab.example.com', 'ansible_loop_var': 'item'}) => {
    "msg": "The result for \"Welcome to serverb.lab.example.com..\""
}
ok: => (item={'content': 'Welcome to wwwb.example.com.', 'redirected': False, 'url': 'http://wwwb.lab.example.com', 'date': 'Sat, 12 Dec 2020 14:16:51 GMT', 'server': 'Apache/2.4.37 (Red Hat Enterprise Linux)', 'last_modified': 'Sat, 12 Dec 2020 14:14:48 GMT', 'etag': '"1c-5b645081685f6"', 'accept_ranges': 'bytes', 'content_length': '28', 'connection': 'close', 'content_type': 'text/html; charset=UTF-8', 'cookies_string': '', 'cookies': {}, 'msg': 'OK (28 bytes)', 'status': 200, 'elapsed': 0, 'changed': False, 'invocation': {'module_args': {'url': 'http://wwwb.lab.example.com', 'return_content': True, 'status_code': ['200'], 'force': False, 'http_agent': 'ansible-httpget', 'use_proxy': True, 'validate_certs': True, 'force_basic_auth': False, 'body_format': 'raw', 'method': 'GET', 'follow_redirects': 'safe', 'timeout': 30, 'headers': {}, 'follow': False, 'url_username': None, 'url_password': None, 'client_cert': None, 'client_key': None, 'dest': None, 'body': None, 'src': None, 'creates': None, 'removes': None, 'unix_socket': None, 'mode': None, 'owner': None, 'group': None, 'seuser': None, 'serole': None, 'selevel': None, 'setype': None, 'attributes': None, 'content': None, 'backup': None, 'remote_src': None, 'regexp': None, 'delimiter': None, 'directory_mode': None, 'unsafe_writes': None}}, 'failed': False, 'item': 'wwwb.lab.example.com', 'ansible_loop_var': 'item'}) => {
    "msg": "The result for \"Welcome to wwwb.example.com..\""
}

PLAY RECAP ************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
serverb                  : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   

$



页: [1]
查看完整版本: ansible11(触发器解决依赖关系)