Ansets set_fact跨玩

时间:2015-06-05 06:48:05

标签: ansible ansible-playbook

我必须运行一个ansible playbook来执行以下任务

1)以YYYY_MM_DD格式计算日期,然后使用此前缀将aws中的某些文件下载到本地计算机。文件名的格式为2015_06_04_latest_file.csv

2)然后我必须将名称为2015_06_04的文件夹创建到多个主机中,然后将该文件上传到那里。

这是我目前的剧本 -

---
- hosts: 127.0.0.1
  connection: local
  sudo: yes
  gather_facts: no
  tasks:
  - name: calculate date
    shell: date "+%Y_%m_%d" --date="1 days ago"
    register: output
  - name: set date variable
    set_fact: latest_date={{ item }}
    with_items: output.stdout_lines
  - local_action: command mkdir -p /tmp/latest_contracts/{{ latest_date }}
  - local_action: command /root/bin/aws s3 cp s3://primarydatafolder/data/{{ latest_date }}_latest_data.csv  /tmp/latest_contracts/{{ latest_date }}/ creates=/tmp/latest_contracts/{{ latest_date }}/latest_data.csv
    register: result
    ignore_errors: true
  - local_action: command /root/bin/aws s3 cp s3://secondarydatafolder/data/{{ latest_date }}_latest_data.csv  /tmp/latest_contracts/{{ latest_date }}/ creates=/tmp/latest_contracts/{{ latest_date }}/latest_data.csv
    when: result|failed
# remove the date prefix from the downloaded file
  - local_action: command ./rename_date.sh {{ latest_date }}
    ignore_errors: true 
- hosts: contractsServers
  sudo: yes
  gather_facts: no
  tasks:
  - name: create directory
    file: path={{item.path}} state=directory mode=0775 owner=root group=root
    with_items:
    - {path: '/var/mukul/contracts/{{ latest_date }}' }
    - {path: '/var/mukul/contracts/dummy' }
  - name: copy dummy contracts
    copy: src=dummy dest=/var/mukul/contracts/
  - name: delete previous symlink
    shell: unlink /var/mukul/contracts/latest
    ignore_errors: true
  - name: upload the newly created latest date folder to the host
    copy: src=/tmp/latest_contracts/{{ latest_date }} dest=/var/mukul/contracts/
  - name: create a symbolic link to the folder on the host and call it latest
    action: file state=link src=/var/mukul/contracts/{{ latest_date }} dest=/var/mukul/contracts/latest

根据ansible关于set_fact变量的文档,此变量 latest_date 应该可用于播放。但是,ansible失败并显示以下消息

failed: [192.168.101.177] => (item={'path': u'/var/mukul/contracts/{# latest_date #}'}) => {"failed": true, "item": {"path": "/var/mukul/contracts/{# latest_date #}"}}
msg: this module requires key=value arguments (['path=/var/mukul/contracts/{#', 'latest_date', '#}', 'state=directory', 'mode=0775', 'owner=root', 'group=root'])

看起来好像第二个剧本无法获得 latest_date 这一事实的价值。你能告诉我我在哪里弄错了吗?

2 个答案:

答案 0 :(得分:12)

事实是特定于主机的。正如documentation about set_fact所说,“[v] ariables [set with set_fact]设置在逐个主机的基础上”。

相反,我会尝试使用Delegation, rolling updates, and local actions中定义的run_once,如下所示:

- hosts: contractsServers
  tasks:
    - name: Determine date
      local_action: shell: date "+%Y_%m_%d" --date="1 days ago"
      register: yesterday
      always_run: True
      changed_when: False
      run_once: True
    - name: Do something else locally
      local_action: ...
      register: some_variable_name
      always_run: True
      changed_when: False
      run_once: True

    - name: Do something remotely using the variables registered above
      ...

答案 1 :(得分:2)

您可以启用fact-caching。您需要设置一个本地redis实例,然后存储事实。