在 Ansible 中设置和使用环境变量

Setting and using environment variable in Ansible

我想在 Ansible 中设置一个值作为环境变量,然后在另一个剧本中使用它。 以下是我的剧本:

get_cmd.yaml

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 

- hosts: localhost
  connection: local
  gather_facts: False

  tasks:

  #- name: Get directory
  #  shell: export ACWD="{{ playbook_dir }}"
  #  when: platform == 'jenkins'

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'

  - name: DEMO
    shell: echo $ACWD

输出

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 
[root@a6296ab33a34 test_code]# ansible-playbook get-cwd.yaml --extra-vars="@deploy-vars.yaml" -vv
 [WARNING] Ansible is being run in a world writable directory (/test_code), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
ansible-playbook 2.8.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: get-cwd.yaml *********************************************************************************************************************************************************************************************************************
1 plays in get-cwd.yaml

PLAY [localhost] ***************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Get CWD] *****************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:11
changed: [localhost] => {"changed": true, "cmd": "export ACWD=/test_code_demo", "delta": "0:00:00.713703", "end": "2019-12-13 14:43:37.054390", "rc": 0, "start": "2019-12-13 14:43:36.340687", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [DEMO] ********************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:15
changed: [localhost] => {"changed": true, "cmd": "echo $ACWD", "delta": "0:00:00.705605", "end": "2019-12-13 14:43:37.919962", "rc": 0, "start": "2019-12-13 14:43:37.214357", "stderr": "", "stderr_lines": [], "stdout": "/test_code_dinesh", "stdout_lines": ["/test_code_dinesh"]}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@a6296ab33a34 test_code]#

你可以看到,虽然我尝试将值设置为test_code_demo,但仍然是旧值test_code_dinesh正在反映。

请告诉我解决上述问题的方法。

请记住,当您设置环境变量时(在任何地方,而不仅仅是在 Ansible 中),它只会影响当前进程及其子进程。

当你运行这样的事情时:

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'

你是:

  1. 生成 shell
  2. 设置环境变量ACWD在那shell
  3. 退出 shell

至此,环境被破坏。无法在一项任务中设置环境变量并使其影响另一项任务。您 可以 使用任务中的 environment 键设置每个任务的环境变量,如下所示:

  - name: DEMO
    shell: echo $ACWD
    environment:
      ACWD: '/test_code_demo'

如果您需要将环境设置应用于多个任务,您可以改为在播放中设置它:

- hosts: localhost
  environment:
    ACWD: '/test_code_demo'
  tasks:
    - command: 'echo $ACWD'
      register: output1

    - command: 'echo $ACWD'
      register: output2

    - debug:
        msg:
          - "{{ output1.stdout }}"
          - "{{ output2.stdout }}"