如何设置注册变量以在ansible中播放之间保持?

Chr*_*ams 52 deployment ansible

我有一个ansible playbook,我想在一台机器上注册的变量在另一台机器上可用.

在我的情况下,我想localhost在这种情况下 运行命令git rev-parse --abbrev-ref HEAD,所以我可以记下当前的git分支和sha1,并注册这个输出,所以我可以在以后工作任何机器时参考它该main集团在第二个上场.

但是,我不清楚如何在localhost上注册变量,所以我可以从main访问它.当我尝试在第二次播放中访问变量时,我收到以下消息:

TASK: [debug msg={{ app_git_sha1.stdout }}] ***********************************
fatal: [main] => One or more undefined variables: 'app_git_sha1' is undefined
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的剧本.有什么明显我应该做的吗?

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: register current branch
      command: git rev-parse --abbrev-ref HEAD
      register: git_branch
      sudo: no
      when: vagrant
      tags:
        - debugsha

    - debug: msg={{ git_branch.stdout }}
      tags:
        - debugsha

    - name: register the SHA1 of the branch being deployed
      command: git rev-parse origin/{{ git_branch.stdout }}
      register: app_git_sha1
      sudo: no
      tags:
        - slack
        - debugsha

    - debug: msg={{ app_git_sha1.stdout }}
      tags:
        - debugsha



- hosts: main
  sudo: yes
  roles:
    - role: productscience.deploy_user
    # TODO reprovision using these roles, for consistency
    # - role: app.essentials
    # - role: zenoamaro.postgresql
    - role: productscience.papertrailapp
    - role: jdauphant.nginx
  tasks:
    - include: setup.yml
    # - include: db.yml

    - name: checkout source control when deploying to remote servers
      include: source.yml
      when: not vagrant
      tags:
          - deploy

    - include: django.yml
      tags:
          - deploy


    - name: include vagrant specific dependencies for local development
      include: vagrant.yml
      when: vagrant

  handlers:
    - name: restart postgres
      sudo: yes
      service: name=postgresql state=restarted
    - name: start restart uwsgi
      sudo: yes
      service: name={{ app }} state=restarted

- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: register the SHA1 of the branch being deployed
      when: not vagrant
      command: git rev-parse origin/{{ git_branch }}
      register: git_sha
      tags:
        - slack

    - name: Send notification message via Slack all options
      when: not vagrant
      tags:
        - slack
      local_action:
        module: slack
        token: "{{ wof_slack_token }}"
        msg: "Deployment of `{{ git_branch }}` to {{ app_url }} completed with sha `{{ git_sha.stdout }}`"
        channel: "#wof"
        username: "Ansible deploy-o-tron"
Run Code Online (Sandbox Code Playgroud)

Bru*_*e P 96

您遇到的问题是您尝试从另一台主机的实体/变量引用一个主机的事实/变量.您需要记住,在Ansible中,app_git_sha1分配给主机localhost的变量不同于app_git_sha1分配给主机main或任何其他主机的变量.如果要从另一个主机访问一个主机事实/变量,则需要通过hostvars变量显式引用它.在这个问题上对此进行了更多的讨论.

假设你有一个像这样的剧本:

- hosts: localhost
  tasks:   
    - command: /bin/echo "this is a test"
      register: foo


- hosts: localhost
  tasks:
    - debug: var=foo
Run Code Online (Sandbox Code Playgroud)

这将起作用,因为您在两个播放中都引用了宿主localhostlocalhosts变量的实例foo.这个剧本的输出是这样的:

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [command /bin/echo "this is a test"] ************************************
changed: [localhost]

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
    "var": {
        "foo": {
            "changed": true,
            "cmd": [
                "/bin/echo",
                "this is a test"
            ],
            "delta": "0:00:00.004585",
            "end": "2015-11-24 20:49:27.462609",
            "invocation": {
                "module_args": "/bin/echo \"this is a test\"",
                "module_complex_args": {},
                "module_name": "command"
            },
            "rc": 0,
            "start": "2015-11-24 20:49:27.458024",
            "stderr": "",
            "stdout": "this is a test",
            "stdout_lines": [
                "this is a test"
            ],
            "warnings": []
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您稍微修改此剧本以在一台主机上运行第一次播放而在另一台主机上播放第二次播放,您将收到您遇到的错误.解决方案是使用Ansible的内置hostvars变量让第二个主机显式引用第一个hosts变量.所以修改第一个例子,如下所示:

- hosts: localhost
  tasks:

    - command: /bin/echo "this is a test"
      register: foo


- hosts: anotherhost
  tasks:
    - debug: var=foo
      when: foo is defined

    - debug: var=hostvars['localhost']['foo']
      when: hostvars['localhost']['foo'] is defined
Run Code Online (Sandbox Code Playgroud)

此剧本的输出显示第一个任务被跳过,因为foo未由主机定义anotherhost.但第二个任务成功,因为它显式引用localhosts变量的实例foo:

TASK: [debug var=foo] *********************************************************
skipping: [anotherhost]

TASK: [debug var=hostvars['localhost']['foo']] **************************
ok: ['anotherhost'] => {
    "var": {
        "hostvars['localhost']['foo']": {
            "changed": true,
            "cmd": [
                "/bin/echo",
                "this is a test"
            ],
            "delta": "0:00:00.005950",
            "end": "2015-11-24 20:54:04.319147",
            "invocation": {
                "module_args": "/bin/echo \"this is a test\"",
                "module_complex_args": {},
                "module_name": "command"
            },
            "rc": 0,
            "start": "2015-11-24 20:54:04.313197",
            "stderr": "",
            "stdout": "this is a test",
            "stdout_lines": [
                "this is a test"
            ],
            "warnings": []
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,简而言之,您希望修改mainPlaybook中的localhost变量引用以这种方式引用变量:

{{ hostvars['localhost']['app_git_sha1'] }}
Run Code Online (Sandbox Code Playgroud)

  • 很棒的答案!这样的东西应该在文档中. (4认同)
  • 如果您使用库存中的主机 - 类似这样的内容应该有帮助 - “{% for host in groups['db_servers'] %}{% if loop.index < 2 %}{{hostvars[host]['myvar'] }}{% endif %}{% endfor %}" (2认同)

mon*_*mon 31

使用虚拟主机及其变量

例如,将K8S令牌和哈希从主服务器传递给工作者.

在主人

- name: "Cluster token"
  shell: kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
  register: K8S_TOKEN

- name: "CA Hash"
  shell: openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  register: K8S_MASTER_CA_HASH

- name: "Add K8S Token and Hash to dummy host"
  add_host:
    name:   "K8S_TOKEN_HOLDER"
    token:  "{{ K8S_TOKEN.stdout }}"
    hash:   "{{ K8S_MASTER_CA_HASH.stdout }}"

- name:
  debug:
    msg: "[Master] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}"

- name:
  debug:
    msg: "[Master] K8S_TOKEN_HOLDER K8S Hash is  {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}"
Run Code Online (Sandbox Code Playgroud)

对工人

- name:
  debug:
    msg: "[Worker] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}"

- name:
  debug:
    msg: "[Worker] K8S_TOKEN_HOLDER K8S Hash is  {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}"

- name: "Kubeadmn join"
  shell: >
    kubeadm join --token={{ hostvars['K8S_TOKEN_HOLDER']['token'] }}
    --discovery-token-ca-cert-hash sha256:{{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}
    {{K8S_MASTER_NODE_IP}}:{{K8S_API_SERCURE_PORT}}
Run Code Online (Sandbox Code Playgroud)

  • 当运行多个 play(在同一个 playbook 上)时,您想要与主机组共享 play 中的变量,例如“hosts: staging,product”,这也非常有价值 (2认同)

kra*_*rad 12

即使是同一个主持人,我也遇到了类似的问题.要记住的事情是事实而不是变量,持续不断的戏剧.以下是我解决问题的方法.

#!/usr/local/bin/ansible-playbook --inventory=./inventories/ec2.py
---
- name: "TearDown Infrastructure !!!!!!!"
  hosts: localhost
  gather_facts: no
  vars:
    aws_state: absent
  vars_prompt:
    - name: "aws_region"
      prompt: "Enter AWS Region:"
      default: 'eu-west-2'
  tasks:
    - name: Make vars persistant
      set_fact:
        aws_region: "{{aws_region}}"
        aws_state: "{{aws_state}}"




- name: "TearDown Infrastructure hosts !!!!!!!"
  hosts: monitoring.ec2
  connection: local
  gather_facts: no
  tasks:
    - name: set the facts per host
      set_fact:
        aws_region: "{{hostvars['localhost']['aws_region']}}"
        aws_state: "{{hostvars['localhost']['aws_state']}}"


    - debug:
        msg="state {{aws_state}} region {{aws_region}} id {{ ec2_id }} "

- name: last few bits
  hosts: localhost
  gather_facts: no
  tasks:
    - debug:
        msg="state {{aws_state}} region {{aws_region}} "
Run Code Online (Sandbox Code Playgroud)

结果是

Enter AWS Region: [eu-west-2]:


PLAY [TearDown Infrastructure !!!!!!!] ***************************************************************************************************************************************************************************************************

TASK [Make vars persistant] **************************************************************************************************************************************************************************************************************
ok: [localhost]

PLAY [TearDown Infrastructure hosts !!!!!!!] *********************************************************************************************************************************************************************************************

TASK [set the facts per host] ************************************************************************************************************************************************************************************************************
ok: [XXXXXXXXXXXXXXXXX]

TASK [debug] *****************************************************************************************************************************************************************************************************************************
ok: [XXXXXXXXXXX] => {
    "changed": false,
    "msg": "state absent region eu-west-2 id i-0XXXXX1 "
}

PLAY [last few bits] *********************************************************************************************************************************************************************************************************************


TASK [debug] *****************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "state absent region eu-west-2 "
}

PLAY RECAP *******************************************************************************************************************************************************************************************************************************
XXXXXXXXXXXXX              : ok=2    changed=0    unreachable=0    failed=0
localhost                  : ok=2    changed=0    unreachable=0    failed=0
Run Code Online (Sandbox Code Playgroud)

  • 是的,只要所有的剧本都在同一个 ansible 调用中。如果您进行多次收集事实,它们显然会被覆盖 (3认同)