Ansible date variable

后端 未结 5 1650
醉酒成梦
醉酒成梦 2020-12-29 01:21

I\'m trying to learn how to use Ansible facts as variables, and I don\'t get it. When I run...

$ ansible localhost -m setup

...it lists all

相关标签:
5条回答
  • 2020-12-29 01:26

    The command ansible localhost -m setup basically says "run the setup module against localhost", and the setup module gathers the facts that you see in the output.

    When you run the echo command these facts don't exist since the setup module wasn't run. A better method to testing things like this would be to use ansible-playbook to run a playbook that looks something like this:

    - hosts: localhost
      tasks:
          - debug: var=ansible_date_time
    
          - debug: msg="the current date is {{ ansible_date_time.date }}"
    

    Because this runs as a playbook facts for localhost are gathered before the tasks are run. The output of the above playbook will be something like this:

    PLAY [localhost] **************************************************
    
    GATHERING FACTS ***************************************************************
    ok: [localhost]
    
    TASK: [debug var=ansible_date_time] *******************************************
    ok: [localhost] => {
        "ansible_date_time": {
            "date": "2015-07-09",
            "day": "09",
            "epoch": "1436461166",
            "hour": "16",
            "iso8601": "2015-07-09T16:59:26Z",
            "iso8601_micro": "2015-07-09T16:59:26.896629Z",
            "minute": "59",
            "month": "07",
            "second": "26",
            "time": "16:59:26",
            "tz": "UTC",
            "tz_offset": "+0000",
            "weekday": "Thursday",
            "year": "2015"
        }
    }
    
    TASK: [debug msg="the current date is {{ ansible_date_time.date }}"] **********
    ok: [localhost] => {
        "msg": "the current date is 2015-07-09"
    }
    
    PLAY RECAP ********************************************************************
    localhost      : ok=3    changed=0    unreachable=0    failed=0
    
    0 讨论(0)
  • The filter option filters only the first level subkey below ansible_facts

    0 讨论(0)
  • 2020-12-29 01:31

    I tried the lookup('pipe,'date') method and got trouble when I push the playbook to the tower. The tower is somehow using UTC timezone. All play executed as early as the + hours of my TZ will give me one day later of the actual date.

    For example: if my TZ is Asia/Manila I supposed to have UTC+8. If I execute the playbook earlier than 8:00am in Ansible Tower, the date will follow to what was in UTC+0. It took me a while until I found this case. It let me use the date option '-d \"+8 hours\" +%F'. Now it gives me the exact date that I wanted.

    Below is the variable I set in my playbook:

      vars:
        cur_target_wd: "{{ lookup('pipe','date -d \"+8 hours\" +%Y/%m-%b/%d-%a') }}"
    

    That will give me the value of "cur_target_wd = 2020/05-May/28-Thu" even I run it earlier than 8:00am now.

    0 讨论(0)
  • 2020-12-29 01:36

    Note that the ansible command doesn't collect facts, but the ansible-playbook command does. When running ansible -m setup, the setup module happens to run the fact collection so you get the facts, but running ansible -m command does not. Therefore the facts aren't available. This is why the other answers include playbook YAML files and indicate the lookup works.

    0 讨论(0)
  • 2020-12-29 01:50

    The lookup module of ansible works fine for me. The yml is:

    - hosts: test
      vars:
        time: "{{ lookup('pipe', 'date -d \"1 day ago\" +\"%Y%m%d\"') }}"
    

    You can replace any command with date to get result of the command.

    0 讨论(0)
提交回复
热议问题