Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible local_action example how does it work

Tags:

ansible

How does the following Ansible play work:

- name: Generate join command
    command: kubeadm token create --print-join-command
    register: join_command

  - name: Copy join command to local file
    local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"

So as I understand, local_action is the same as delegate_to but, copy content= did not make any sense. Isn't the actual command like "cp" need to be specified?

Take this example: local_action: command ping -c 1 {{ inventory_hostname }}

Can we use something like this:

local_action: command cp content="{{ join_command.stdout_lines[0] }}" dest="./join-command"

like image 410
kamal Avatar asked May 08 '19 20:05

kamal


2 Answers

So as I understand, local_action is the same as delegate_to ...

local_action is similar to delegate_to: localhost, but because local_action requires you to change the syntax of tasks, it's better to always use delegate_to. That is, while for a standard task, you might write:

- name: copy a file
  copy:
    src: myfile
    dest: /path/to/myfile

And for a delegated task, you would use exactly the same syntax with the addition of the delegate_to line:

- name: copy a file
  delegate_to: localhost
  copy:
    src: myfile
    dest: /path/to/myfile

When using local_action you have to change the syntax of the task:

- name: copy a file
  local_action:
    module: copy
    src: myfile
    dest: /path/to/myfile

copy content= did not make any sense. Isn't the actual command like "cp" need to be specified?

No, copy is the name of an ansible module. You can see some examples of above, or just take a look at the documentation.

so looking at an example: local_action: command ping -c 1 {{ inventory_hostname }} Can we say:

local_action: command cp content="{{ join_command.stdout_lines[0] }}" dest="./join-command"

You should use delegate_to, and you should write it like this:

- delegate_to: localhost
  copy:
    content: "{{ join_command.stdout_lines[0] }}"
    dest: ./join-command

Or if you actually want to run the cp command instead of the copy module, you would write something like:

- delegate_to: localhost
  command: cp some_file another_file

That is simply running the standard cp command, which knows nothing about content= or dest=.

like image 124
larsks Avatar answered Nov 15 '22 11:11

larsks


Hard to say what's the path to dest="./join-command". The full path works as expected.

For example:

dest="{{ playbook_dir }}/join-command"
like image 26
Vladimir Botka Avatar answered Nov 15 '22 11:11

Vladimir Botka