【问题标题】:Create Local File With Ansible Template From Variables使用 Ansible 模板从变量创建本地文件
【发布时间】:2022-08-19 20:34:23
【问题描述】:

我正在针对多个 ec2 实例运行 ansible playbook 以检查目录是否存在。

---
- hosts: all
  become: true
  tasks:
  - name: Check if foo is installed
    stat:
      path:
        /etc/foo
    register: path
  - debug: msg=\"{{path.stat.exists}}\"

我想生成一个本地文件,列出 ec2 实例的私有 IP 地址,并说明目录 foo 是否存在。

我可以通过此任务获取实例的私有 IP 地址

  - name: Get info from remote
    shell: curl http://169.254.169.254/latest/meta-data/local-ipv4
    register: bar
  - debug: msg=\"{{bar.stdout}}\"

如何创建包含内容的本地文件

IP address: 10.100.0.151 directory foo - false
IP address: 10.100.0.152 directory foo - true

我已经尝试为此添加一个块

- hosts: localhost
  become: false
  vars:
    installed: \"{{bar.stdout}}\"
    status:    \"{{path.stat.exists}}\"
    local_file: \"./Report.txt\"
  tasks:

  - name: Create local file with info
    copy:
      dest: \"{{ local_file }}\"
      content: |
        \"IP address {{ installed }} foo - {{ status }}\"

但看起来我不能从前面的步骤中读取变量的值。

请问我做错了什么?

    标签: ansible ansible-2.x ansible-template


    【解决方案1】:

    here 已经回答了类似的问题。

    基本上你想要的是通过主机 var 变量来引用它。

    这应该有效。

    - hosts: localhost
      become: false
      vars:
        local_file: "./Report.txt"
      tasks:
    
      - name: Create local file with info
        lineinfile:
          path: "{{ local_file }}"
          line:
            "IP Address: {{ hostvars[item]['bar'].stdout }} - Installed: {{ hostvars[item]['path'].stat.exists }}"
        with_items: "{{ query('inventory_hostnames', 'all') }}"
    

    这应该使用您需要的信息填充您的本地 ./Report.txt 文件。

    【讨论】:

      【解决方案2】:

      我使用ec2_metadata_facts module 来获取使用ansible_ec2_local_ipv4 的IP 地址
      我还在第二台主机上创建了目录 /tmp/testdir。

      - hosts: test_hosts
        gather_facts: no
        vars:
          directory_name: /tmp/testdir
        tasks:
      
        - ec2_metadata_facts:
      
        - name: check if directory '{{ directory_name }}' exsists
          stat:
            path: "{{ directory_name }}"
          register: path
      
      
        # I make the outputfile empty
        # because the module lineinfile(as I know) can't overwrite a file
        # but appends line to the old content
        - name: create empty output file
          copy:
            content: ""
            dest: outputfile
          delegate_to: localhost
          
        - name: write output to outputfile
          lineinfile:
            dest: outputfile
            line: "IP Address: {{ ansible_ec2_local_ipv4 }} {{ directory_name }} - {{ path.stat.exists }}"
            state: present
          with_items: "{{ groups.all }}"
          # with_items: "{{ ansible_play_hosts }}" can also be used here
          delegate_to: localhost
      

      输出文件如下所示:

      IP Address: xxx.xx.x.133 /tmp/testdir - False
      IP Address: xxx.xx.x.45 /tmp/testdir - True
      

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2020-03-10
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多