-1

i am running below task and replacing whole content in the destination file with ip address

---
- hosts: localhost
  connection: local
  tasks:
    - debug: var=ansible_all_ipv4_addresses
    - debug: var=ansible_default_ipv4.address
    - copy: content="{{ ansible_all_ipv4_addresses }}" dest=/root/curator.yml

i have variable in curator.yml, i want to update variable {{ ansible_default_ipv4.address }} with ip address.

---
client:
  hosts:
    - {{ ansible_default_ipv4.address }}
  port: 9200
  url_prefix:
  use_ssl: False
  ssl_no_validate: False
  http_auth:
  timeout: 30
  master_only: False

logging:
  loglevel: INFO
  logfile:
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

When i execute above playbook task it's replacing whole information in curator.yml with ip address in the debug output

PLAY [localhost] ****************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************** ok: [localhost]

TASK [debug] ******************************************************************************************************************** ok: [localhost] => { "ansible_all_ipv4_addresses": [ "10.0.0.5" ] }

TASK [debug] ******************************************************************************************************************** ok: [localhost] => { "ansible_default_ipv4.address": "10.0.0.5" }

TASK [copy] ********************************************************************************************************************* changed: [localhost]

PLAY RECAP ********************************************************************************************************************** localhost : ok=4 changed=1 unreachable=0 failed=0

i am also included below task but looks like it's not working

#- name: rewrite
#  vars:
#    ansible_default_ipv4.address: "{{ ansible_default_ipv4.address[0] }}"
#  template:
#    src: templates/curator.yml.j2
#    dest: /root/curator.yml
Vijaya Krishna
  • 35
  • 1
  • 12

1 Answers1

0

There are issues with your copy and template tasks:

  1. Copy Task - As you are using content parameter, it will "set the contents of a file directly to the specified value" copy_module

  2. Template task - You can't define/update vars (or even set_fact) with .("dot") notation and you don't even need to, as the ansible_default_ipv4.address variable is already defined and the value is set.

This will work:

---
- name: Update ip
  hosts: 127.0.0.1
  connection: local
  tasks:
    - debug: var=ansible_all_ipv4_addresses
    - debug: var=ansible_default_ipv4.address
    - name: Template file with ip
      template:
        src: templates/curator.yml.j2
        dest: /root/curator.yml
...
Anant Naugai
  • 538
  • 4
  • 14
  • Thanks, it works for me when i run task alone. When i included with other task it complaining. The error appears to have been in '/jenkins/var/lib/tasks/main.yml': line 15, column 3, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: Update ip ^ here – Vijaya Krishna Nov 30 '18 at 19:08