Ansible 如何将一位数传递给下一个命令

Ansible 如何将一位数传递给下一个命令,ansible,Ansible,我正在尝试将RHEL版本传递给下一个命令,但我无法: 我的脚本如下: ### To Create Repo File - name: Check for the presence of the DIR. below stat: path=/etc/yum.repos.d register: dir - shell: mkdir -p /etc/yum.repos.d when: not dir.stat.exists - name:

我正在尝试将RHEL版本传递给下一个命令,但我无法:

我的脚本如下:

### To Create Repo File

    - name: Check for the presence of the DIR. below
      stat: path=/etc/yum.repos.d
      register: dir
    - shell: mkdir -p /etc/yum.repos.d
      when: not dir.stat.exists
    - name: To get the RHEL Release
      shell: cat /etc/redhat-release | awk '{ print $7 }' |cut -c1
      register: RH_REL
#
    - shell: touch /etc/yum.repos.d/patch.repo
    - shell: echo [RHEL-{{ RH_REL }}-2020_{{ WW }}-OS] > /etc/yum.repos.d/patch.repo
ok: [dmzpnet6.wellsfargo.net] => {
    "RH_REL.stdout": "7"
}
我得到的调试结果如下:

### To Create Repo File

    - name: Check for the presence of the DIR. below
      stat: path=/etc/yum.repos.d
      register: dir
    - shell: mkdir -p /etc/yum.repos.d
      when: not dir.stat.exists
    - name: To get the RHEL Release
      shell: cat /etc/redhat-release | awk '{ print $7 }' |cut -c1
      register: RH_REL
#
    - shell: touch /etc/yum.repos.d/patch.repo
    - shell: echo [RHEL-{{ RH_REL }}-2020_{{ WW }}-OS] > /etc/yum.repos.d/patch.repo
ok: [dmzpnet6.wellsfargo.net] => {
    "RH_REL.stdout": "7"
}
但我只需要在输出中得到6或7,就可以传递那个一位数,或者我可以使用其他东西来代替“寄存器”

多谢各位

迈克尔

问:“我只需要得到6或7……或者我可以用其他东西代替‘注册’?”

A:是的。您可能需要使用模块创建的变量
ansible\u distribution\u major\u version
。没有必要显式地运行它。默认情况下,启动时将收集事实,除非您要求收集事实:false。比如说

- hosts: remote_hosts
  gather_facts: true
  tasks:
    - debug:
        var: ansible_distribution_major_version
    - shell: "echo {{ ansible_distribution_major_version }} > /tmp/my_ansible_test.txt"

查看文件
/tmp/my_ansible_test.txt
的内容,位于
remote_hosts
“RH_REL.stdout”:“7”
-
但我只需要在输出中得到6或7,这不是你得到的吗?是的,但我只需要得到:7没有:RH_REL.stdout:“7更多,请查看文档,更具体地说,模块,即。。。。以上所有任务都可以替换为对这些任务的幂等调用。因此,还有一个文档:。使用选项
msg:“{{RH_REL.stdout}”
。但是你不需要从登记簿上拿到这个
ansible\u distribution\u major\u version
如果您收集了事实=>它会得到ansible版本吗?我正在寻找RedHat版本,我得到了它并得到了正确的RHEL 6或7版本,因此,我没有将它重定向到文件并进行调试,而是如何将它定向到我的命令中:是的,它正在工作,谢谢,但是现在我没有将它重定向到文件中,而是如何将6或7重定向到我的命令:-shell:echo[RHEL-{{RH_REL}-2020{WW}-OS]>/etc/yum.repos.d/patch.repoholl,它正在工作,非常感谢您的帮助。不客气。这些评论不包含任何有用的信息,可能会被删除。这会让其他人阅读起来更简单。这不是针对个人的。这是如何工作的。请参阅。删除评论。