Assuming you connect as user 'ansible', this playbook will do what you want:

---
- hosts: centos

  tasks:
    - name: Populate file with hostname and IPv4
      copy:
        dest: hostname
        content: "{{ ansible_default_ipv4.address }}\n{{ ansible_hostname
}}"




On Mon, 27 Apr 2020 at 07:11, P r @ v i N <[email protected]> wrote:

> Its linux academy, video based...no trainer as such.
>
> On Sun, Apr 26, 2020, 22:10 Dick Visser <[email protected]> wrote:
>
>> Which training is this?
>> Usually with a training you need to come up with answers to questions
>> yourself, and the trainer shows you the answer if you don't know it.
>>
>>
>>
>> On Sun, 26 Apr 2020 at 18:33, Pravin Patil <[email protected]> wrote:
>>
>>> This is part of the training module I am currently learning, to try how
>>> the magic variables work.
>>> the centos is group, with one host, in the inventory file.
>>> the output of this playbook is supposed to be a file named "hostname"
>>> gets created on target host. And it should have two values:
>>> "ansible_hostname", and the "ipv4" address.
>>>
>>>
>>>
>>>
>>>
>>> On Sunday, 26 April 2020 09:24:08 UTC-7, Dick Visser wrote:
>>>
>>>> If you want to create files then I would suggest using the template
>>>> module, or even copy.
>>>>
>>>> You are targeting a group, but are using hostvars- what is your actual
>>>> intention? A group, a host, or...? This is necessary to answer your
>>>> question.
>>>>
>>>> BTW attaching screendumps add unnecessary clutter for people and make
>>>> it difficult for content to be indexed. So don't use screendumps but just
>>>> text.
>>>>
>>>>
>>>>
>>>>
>>>> On Sun, 26 Apr 2020 at 18:07, Pravin Patil <[email protected]> wrote:
>>>>
>>> Hello -
>>>>> I am trying to create a new file called 'hostname", and within it
>>>>> trying to copy the ansible_hostname, and ipv4 address using the hostvars
>>>>> magic variable.
>>>>> Attaching the screenshots.
>>>>>
>>>>>
>>>>>
>>>>> [image: Screen Shot 2020-04-26 at 9.06.21 AM.png]
>>>>>
>>>>> [image: Screen Shot 2020-04-26 at 9.06.43 AM.png]
>>>>>
>>>>>
>>>>> On Saturday, 25 April 2020 21:54:14 UTC-7, Dick Visser wrote:
>>>>>>
>>>>>> Hi
>>>>>>
>>>>>> I don't see attached files (which is good I'd say) but then the
>>>>>> indentation of the include yaml files is a bit messed up.
>>>>>>
>>>>>> Regardless of that, it is not clear what you are trying to do with
>>>>>> this playbook?
>>>>>> Write the default ipv4 address of all hosts in a group to a file on
>>>>>> all those hosts?
>>>>>> Please explain what you want to do, and why (as there may be
>>>>>> alternative way to achieve what you want).
>>>>>>
>>>>>> On Sun, 26 Apr 2020 at 04:01, Pravin Patil <[email protected]> wrote:
>>>>>>
>>>>> Please refer to the attached file for issues I am running into...
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *Here is my playbook:*
>>>>>>>
>>>>>>> --- # Ansible facts example
>>>>>>>
>>>>>>> - hosts: centos
>>>>>>>
>>>>>>>  tasks:
>>>>>>>
>>>>>>>      - name: create a file to write the fact values
>>>>>>>
>>>>>>>       lineinfile:
>>>>>>>
>>>>>>>           path: /home/ansible/hostname
>>>>>>>
>>>>>>>           create: yes
>>>>>>>
>>>>>>>           line: "{{ ansible_hostname }}"
>>>>>>>
>>>>>>>      - name: access magic variables
>>>>>>>
>>>>>>>       lineinfile:
>>>>>>>
>>>>>>>           path: /home/ansible/hostname
>>>>>>>
>>>>>>>           line: "{{
>>>>>>> hostvars['centos']['ansible_default_ipv4']['address'] }}"
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *And here is the command I am using to run it:*
>>>>>>>
>>>>>>> ansible-playbook -i inv facts.yml
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *And here is the inventory (host):*
>>>>>>>
>>>>>>> [centos]
>>>>>>>
>>>>>>> cen ansible_host=172.31.4.215
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *I keep running into this error:*
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> PLAY [centos]
>>>>>>> ********************************************************************
>>>>>>>
>>>>>>> TASK [Gathering Facts]
>>>>>>> ***********************************************************
>>>>>>>
>>>>>>> ok: [cen]
>>>>>>>
>>>>>>> TASK [create a file to write the fact values]
>>>>>>> ************************************
>>>>>>>
>>>>>>> ok: [cen]
>>>>>>>
>>>>>>> TASK [access magic variables]
>>>>>>> ****************************************************
>>>>>>>
>>>>>>> fatal: [cen]: FAILED! => {"msg": "The task includes an option with
>>>>>>> an undefined variable. The error was: \"hostvars['centos']\" is
>>>>>>> undefined\n\nThe error appears to have been in 
>>>>>>> '/home/ansible/facts.yml':
>>>>>>> line 9, column 13, but may\nbe elsewhere in the file depending on the 
>>>>>>> exact
>>>>>>> syntax problem.\n\nThe offending line appears to be:\n\n          line:
>>>>>>> \"{{ ansible_hostname }}\"\n     - name: access magic variables\n      ^
>>>>>>> here\n"}
>>>>>>>
>>>>>>> to retry, use: --limit @/home/ansible/facts.retry
>>>>>>>
>>>>>>> PLAY RECAP
>>>>>>>
>>>>>>>
>>>>>>> ***********************************************************************
>>>>>>>
>>>>>>> cen            : ok=2   changed=0  unreachable=0  failed=1
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *How to resolve this?*
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Ansible Project" group.
>>>>>>>
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> To view this discussion on the web visit
>>>>>>> https://groups.google.com/d/msgid/ansible-project/b4e1237d-9f80-4fee-8b84-61116466c7db%40googlegroups.com
>>>>>>> <https://groups.google.com/d/msgid/ansible-project/b4e1237d-9f80-4fee-8b84-61116466c7db%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>> --
>>>>>> Sent from a mobile device - please excuse the brevity, spelling and
>>>>>> punctuation.
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>>
>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/c5fc6658-e6b7-4653-8446-cb50f57bf279%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/ansible-project/c5fc6658-e6b7-4653-8446-cb50f57bf279%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> Sent from a mobile device - please excuse the brevity, spelling and
>>>> punctuation.
>>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/7e2999f7-5ae8-4ce4-8e4c-e14e2b5bf185%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/7e2999f7-5ae8-4ce4-8e4c-e14e2b5bf185%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> Sent from a mobile device - please excuse the brevity, spelling and
>> punctuation.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Ansible Project" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/ansible-project/aaiWW_kuNuk/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/CAL8fbwOG-s%2BUNgKd52kwXT%2BtZy-%3D6tzKVrxWn_eJFh-yMnQcAQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/ansible-project/CAL8fbwOG-s%2BUNgKd52kwXT%2BtZy-%3D6tzKVrxWn_eJFh-yMnQcAQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAB9su_AN2X04e-_Ea%3DXpV2Jm7TfmohYKA%3DCKPeJ80Y9prgM7Cg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAB9su_AN2X04e-_Ea%3DXpV2Jm7TfmohYKA%3DCKPeJ80Y9prgM7Cg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL8fbwOAGu%2Bmtq6hceRFsp4dqnh7Ng4GRy8%3DzpVB9a%3Dco2amjw%40mail.gmail.com.

Reply via email to