1

I want to execute a command using ansible 2.9.10 in remote machine, first I tried like this:

ansible kubernetes-root -m command -a "cat > /etc/docker/daemon.json <<EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-size": "100m"
    },
    "storage-driver": "overlay2",
    "registry-mirrors":[
        "https://kfwkfulq.mirror.aliyuncs.com",
        "https://2lqq34jg.mirror.aliyuncs.com",
        "https://pee6w651.mirror.aliyuncs.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}"

obviously it is not working.so I read this guide and tried like this:

- hosts: kubernetes-root
  remote_user: root
  tasks:
    - name: add docker config
      shell: >
      cat > /etc/docker/daemon.json <<EOF
      {
          "exec-opts": ["native.cgroupdriver=systemd"],
          "log-driver": "json-file",
          "log-opts": {
          "max-size": "100m"
          },
          "storage-driver": "overlay2",
          "registry-mirrors":[
              "https://kfwkfulq.mirror.aliyuncs.com",
              "https://2lqq34jg.mirror.aliyuncs.com",
              "https://pee6w651.mirror.aliyuncs.com",
              "http://hub-mirror.c.163.com",
              "https://docker.mirrors.ustc.edu.cn",
              "https://registry.docker-cn.com"
          ]
      }

and execute it like this:

 [dolphin@MiWiFi-R4CM-srv playboook]$ ansible-playbook add-docker-config.yaml 
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  could not find expected ':'

The error appears to be in '/home/dolphin/source-share/source/dolphin/dolphin-scripts/ansible/playboook/add-docker-config.yaml': line 7, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      cat > /etc/docker/daemon.json <<EOF
      {
      ^ here

is there anyway to achive this?how to fix it?

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • You'd better use `copy` or `template` module instead of using `shell` to cat a content redirecting to a file. The file is going to be interpreted into different languages (ansible->jinja->shell), and possibly doing something wrong. Moreover it makes no sense when you use automation tooling to do that, you loose indempotency, dry-run mode, .... my advice: I think you'd re-think how ansible is meant to be used. – Baptiste Mille-Mathias Jul 06 '20 at 08:57

1 Answers1

2

your playbook should work fine, you just have to add some indentation after the shell clause line, and change the > to |:

here is the updated PB:

---
- name: play name
  hosts: dell420
  gather_facts: false
  vars:


  tasks:
  - name: run shell task
    shell: |
      cat > /tmp/temp.file << EOF 
      {
          "exec-opts": ["native.cgroupdriver=systemd"],
          "log-driver": "json-file",
          "log-opts": {
          "max-size": "100m"
          },
          "storage-driver": "overlay2",
          "registry-mirrors":[
              "https://kfwkfulq.mirror.aliyuncs.com",
              "https://2lqq34jg.mirror.aliyuncs.com",
              "https://pee6w651.mirror.aliyuncs.com",
              "http://hub-mirror.c.163.com",
              "https://docker.mirrors.ustc.edu.cn",
              "https://registry.docker-cn.com"
          ]
      }
      EOF

Not sure what is wrong with the ad-hoc command, i tried a few things but didnt manage to make it work.

hope these help

EDIT:

as pointed out by Zeitounator, the ad-hoc command will work if you use shell module instead of command. example:

ansible -i hosts dell420 -m shell -a 'cat > /tmp/temp.file <<EOF
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-size": "100m"
    },
    "storage-driver": "overlay2",
    "registry-mirrors":[
        "https://kfwkfulq.mirror.aliyuncs.com",
        "https://2lqq34jg.mirror.aliyuncs.com",
        "https://pee6w651.mirror.aliyuncs.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}
EOF
'
ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • 2
    The ad-hoc command should work as well when using the `shell` module as in the playbook instead of `command` which does not support output redirection, piping, etc. – Zeitounator Jul 05 '20 at 19:43
  • 2
    you are right @Zeitounator, updated the answer with your input! – ilias-sp Jul 05 '20 at 20:10