Answers: <p>In my case it ended up that Jenkins was running docker agent with specific UID and GID. To get that fixed it required to rebuild that docker image with creating internal Jenkins user with the same UID and GID</p>
  <p>For that purpose on top of the Jenkinsfile to crate that docker image I have added:</p>
  <pre><code>def user_id
  def group_id
  node {
     user_id = sh(returnStdout: true, script: 'id -u').trim()
     group_id = sh(returnStdout: true, script: 'id -g').trim()
  }
  
  </code></pre>
  <p>and then during the build stage I have passed additional arguments to the docker as</p>
  <pre><code>--build-arg JenkinsUserId=${user_id} --build-arg JenkinsGroupId=${group_id}
  </code></pre>
  <p>then in the Dockerfile for that build:</p>
  <pre><code>FROM alpine:latest
  
  #pick up provided ARGs for the bild
  
  ARG JenkinsUserId
  ARG JenkinsGroupId
  
  //do your stuff here
  
  #create Ansible config directory
  RUN set -xe \
      &amp;&amp; mkdir -p /etc/ansible
  
  #create Ansible tmp directory
  RUN set -xe \
      &amp;&amp; mkdir -p /.ansible/tmp
  
  #set ANSIBLE_LOCAL_TEMP
  ENV ANSIBLE_LOCAL_TEMP /.ansible/tmp
  
  #create Ansible cp directory
  RUN set -xe \
      &amp;&amp; mkdir -p /.ansible/cp
  
  #set ANSIBLE_SSH_CONTROL_PATH_DIR
  ENV ANSIBLE_SSH_CONTROL_PATH_DIR /.ansible/cp
  
  # Create Jenkins group and user
  RUN if ! id $JenkinsUserId; then \
      groupadd -g ${JenkinsGroupId} jenkins; \
      useradd jenkins -u ${JenkinsUserId} -g jenkins --shell /bin/bash --create-home; \
    else \
      addgroup --gid 1000 -S jenkins &amp;&amp; adduser --uid 1000 -S jenkins -G jenkins; \
    fi
  
  RUN addgroup jenkins root
  
  # Tell docker that all future commands should run as the appuser user
  USER jenkins
  </code></pre>
  <p>and finally update <code>docker</code> <code>agent</code> for the main pipeline which had issue:</p>
  <pre><code>   agent {
        docker {
           image 'artifactory.devbox.local/docker-local/myrepo/jdk8:latest'
           args '-v $HOME/.m2:/root/.m2 -v $HOME/.ssh:/home/jenkins/.ssh  -v /etc/ansible:/etc/ansible -v $HOME/.ansible/tmp:/.ansible/tmp -v $HOME/.ansible/cp:/.ansible/cp'
        }
     }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-10-09T20:31:40.660Z
Answers: 2020-10-09T20:31:40.660Z
Answers: JackTheKnife
Body: <p>I'm trying to deploy a build via Jenkins pipeline using <code>agent</code> <code>docker</code> and Ansible playbook but it fails on <code>Gathering Facts</code> stage as shown below:</p>
  <pre><code>TASK [Gathering Facts] *********************************************************
  fatal: [destination.box.local]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;argument must be an int, or have a fileno() method.&quot;, &quot;unreachable&quot;: true}
  </code></pre>
  <p>Similar Jenkins pipeline using <code>agent</code> <code>any</code> and Ansible not from docker (local installation) will do the job w/o any hiccups.</p>
  <p>Agent section from Jenkins pipeline looks like:</p>
  <pre><code>pipeline {
  
      agent {
         docker {
            image 'artifactory.devbox.local/docker-local/myrepo/jdk8:latest'
            args '-v $HOME/.m2:/root/.m2 -v /etc/ansible:/etc/ansible -v $HOME/.ansible/tmp:/.ansible/tmp -v $HOME/.ssh:/root/.ssh'
         }
      }
  
     parameters {
        string(
                name: 'PROFILE',
                defaultValue: 'dev',
                description: 'Select profile to build?'
        )
        string(
                name: 'HOST',
                defaultValue: 'dev',
                description: 'Which Outbound Host(s) group to deploy to?'
        )
     }
  
     stages {
  
        stage('Artifactory configuration') {
           steps {
              rtMavenDeployer(
                      id: &quot;MAVEN_DEPLOYER&quot;,
                      serverId: 'Artifactory',
                      releaseRepo: &quot;libs-release-local&quot;,
                      snapshotRepo: &quot;libs-snapshot-local&quot;
              )
  
              rtMavenResolver(
                      id: &quot;MAVEN_RESOLVER&quot;,
                      serverId: 'Artifactory',
                      releaseRepo: &quot;libs-release&quot;,
                      snapshotRepo: &quot;libs-snapshot&quot;
              )
           }
        }
  
        stage('Maven build') {
           steps {
              withEnv(['JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk', 'MAVEN_HOME=/usr/share/maven']) {
                 rtMavenRun(
                         pom: 'pom.xml',
                         goals: 'clean',
                         deployerId: &quot;MAVEN_DEPLOYER&quot;,
                         resolverId: &quot;MAVEN_RESOLVER&quot;
                 )
              }
           }
        }
  
        stage ('Publish build') {
           steps {
              rtPublishBuildInfo (
                      serverId: 'Artifactory',
              )
           }
        }
  
        stage('deploy') {
           when{
              anyOf {
                 branch 'master'
                 branch 'develop'
              }
           }
           steps {
              ansiblePlaybook(
                      playbook: 'playbook.yml',
                      extraVars: [
                              profile: &quot;${params.PROFILE}&quot;,
                              host: &quot;${params.HOST}&quot;
                      ]
              )
           }
        }
  
     }
  }
  </code></pre>
  <p>After adding <code>ansible_ssh_common_args='-o StrictHostKeyChecking=no'</code> to the Ansible inventory (or setting <code>host_key_checking = False</code> in the config) I have got that error:</p>
  <pre><code>TASK [Gathering Facts] *********************************************************
  fatal: [destination.box.local]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;'getpwuid(): uid not found: 700'&quot;, &quot;unreachable&quot;: true}
  fatal: [ansible_ssh_common_args=-o StrictHostKeyChecking=no]: UNREACHABLE! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;[Errno -3] Try again&quot;, &quot;unreachable&quot;: true}
  </code></pre>
  <p>Any thought what I need to add to it to let Ansible run a playbook?</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-10-08T07:34:11.673Z
Comments: I assume that you're using declarative pipeline. Anyway, I don't see any `steps` in your pipeline that runs Ansible. Can you five us the full Jenkinsfile please ?
Comments: Hedi Nasr
Comments: CC BY-SA 4.0
Comments: 2020-10-08T12:39:41.557Z
Comments: @HediNasr OP updated with Jenkinsfile
Comments: JackTheKnife
Tags: <docker><jenkins><ansible>
Title: Ansible fails in Jenkins pipeline when run from docker
UserDisplayName: JackTheKnife
ThreadText: # Ansible fails in Jenkins pipeline when run from docker
  
  (Asked by: username_0 on 2020-10-07)
  
  I'm trying to deploy a build via Jenkins pipeline using `agent` `docker` and Ansible playbook but it fails on `Gathering Facts` stage as shown below:
  
  ```
  TASK [Gathering Facts] *********************************************************
  fatal: [destination.box.local]: UNREACHABLE! => {"changed": false, "msg": "argument must be an int, or have a fileno() method.", "unreachable": true}
  
  ```
  
  Similar Jenkins pipeline using `agent` `any` and Ansible not from docker (local installation) will do the job w/o any hiccups.
  
  Agent section from Jenkins pipeline looks like:
  
  ```
  pipeline {
  
      agent {
         docker {
            image 'artifactory.devbox.local/docker-local/myrepo/jdk8:latest'
            args '-v $HOME/.m2:/root/.m2 -v /etc/ansible:/etc/ansible -v $HOME/.ansible/tmp:/.ansible/tmp -v $HOME/.ssh:/root/.ssh'
         }
      }
  
     parameters {
        string(
                name: 'PROFILE',
                defaultValue: 'dev',
                description: 'Select profile to build?'
        )
        string(
                name: 'HOST',
                defaultValue: 'dev',
                description: 'Which Outbound Host(s) group to deploy to?'
        )
     }
  
     stages {
  
        stage('Artifactory configuration') {
           steps {
              rtMavenDeployer(
                      id: "MAVEN_DEPLOYER",
                      serverId: 'Artifactory',
                      releaseRepo: "libs-release-local",
                      snapshotRepo: "libs-snapshot-local"
              )
  
              rtMavenResolver(
                      id: "MAVEN_RESOLVER",
                      serverId: 'Artifactory',
                      releaseRepo: "libs-release",
                      snapshotRepo: "libs-snapshot"
              )
           }
        }
  
        stage('Maven build') {
           steps {
              withEnv(['JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk', 'MAVEN_HOME=/usr/share/maven']) {
                 rtMavenRun(
                         pom: 'pom.xml',
                         goals: 'clean',
                         deployerId: "MAVEN_DEPLOYER",
                         resolverId: "MAVEN_RESOLVER"
                 )
              }
           }
        }
  
        stage ('Publish build') {
           steps {
              rtPublishBuildInfo (
                      serverId: 'Artifactory',
              )
           }
        }
  
        stage('deploy') {
           when{
              anyOf {
                 branch 'master'
                 branch 'develop'
              }
           }
           steps {
              ansiblePlaybook(
                      playbook: 'playbook.yml',
                      extraVars: [
                              profile: "${params.PROFILE}",
                              host: "${params.HOST}"
                      ]
              )
           }
        }
  
     }
  }
  
  ```
  
  After adding `ansible_ssh_common_args='-o StrictHostKeyChecking=no'` to the Ansible inventory (or setting `host_key_checking = False` in the config) I have got that error:
  
  ```
  TASK [Gathering Facts] *********************************************************
  fatal: [destination.box.local]: UNREACHABLE! => {"changed": false, "msg": "'getpwuid(): uid not found: 700'", "unreachable": true}
  fatal: [ansible_ssh_common_args=-o StrictHostKeyChecking=no]: UNREACHABLE! => {"changed": false, "msg": "[Errno -3] Try again", "unreachable": true}
  
  ```
  
  Any thought what I need to add to it to let Ansible run a playbook?
  
  **Tags:** docker, jenkins, ansible
  
  > **Comments:**
  > **username_1**: I assume that you're using declarative pipeline. Anyway, I don't see any `steps` in your pipeline that runs Ansible. Can you five us the full Jenkinsfile please ?  
  > **username_0**: @username_1 OP updated with Jenkinsfile  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-10-09. Score: 0 | Accepted answer
  
  In my case it ended up that Jenkins was running docker agent with specific UID and GID. To get that fixed it required to rebuild that docker image with creating internal Jenkins user with the same UID and GID
  
  For that purpose on top of the Jenkinsfile to crate that docker image I have added:
  
  ```
  def user_id
  def group_id
  node {
     user_id = sh(returnStdout: true, script: 'id -u').trim()
     group_id = sh(returnStdout: true, script: 'id -g').trim()
  }
  
  ```
  
  and then during the build stage I have passed additional arguments to the docker as
  
  ```
  --build-arg JenkinsUserId=${user_id} --build-arg JenkinsGroupId=${group_id}
  
  ```
  
  then in the Dockerfile for that build:
  
  ```
  FROM alpine:latest
  
  #pick up provided ARGs for the bild
  
  ARG JenkinsUserId
  ARG JenkinsGroupId
  
  //do your stuff here
  
  #create Ansible config directory
  RUN set -xe \
      && mkdir -p /etc/ansible
  
  #create Ansible tmp directory
  RUN set -xe \
      && mkdir -p /.ansible/tmp
  
  #set ANSIBLE_LOCAL_TEMP
  ENV ANSIBLE_LOCAL_TEMP /.ansible/tmp
  
  #create Ansible cp directory
  RUN set -xe \
      && mkdir -p /.ansible/cp
  
  #set ANSIBLE_SSH_CONTROL_PATH_DIR
  ENV ANSIBLE_SSH_CONTROL_PATH_DIR /.ansible/cp
  
  # Create Jenkins group and user
  RUN if ! id $JenkinsUserId; then \
      groupadd -g ${JenkinsGroupId} jenkins; \
      useradd jenkins -u ${JenkinsUserId} -g jenkins --shell /bin/bash --create-home; \
    else \
      addgroup --gid 1000 -S jenkins && adduser --uid 1000 -S jenkins -G jenkins; \
    fi
  
  RUN addgroup jenkins root
  
  # Tell docker that all future commands should run as the appuser user
  USER jenkins
  
  ```
  
  and finally update `docker` `agent` for the main pipeline which had issue:
  
  ```
     agent {
        docker {
           image 'artifactory.devbox.local/docker-local/myrepo/jdk8:latest'
           args '-v $HOME/.m2:/root/.m2 -v $HOME/.ssh:/home/jenkins/.ssh  -v /etc/ansible:/etc/ansible -v $HOME/.ansible/tmp:/.ansible/tmp -v $HOME/.ansible/cp:/.ansible/cp'
        }
     }
  
  ```
  
  
  ---
  
  